forked from gonativeio/gonative-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-theme.js
executable file
·129 lines (110 loc) · 4.97 KB
/
generate-theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
var DARK_DEFAULT_ACTIONBAR_COLOR = '212121';
var DARK_DEFAULT_STATUSBAR_COLOR = '030303';
var DARK_DEFAULT_TITLE_COLOR = 'ffffff';
var DARK_DEFAULT_ACCENT_COLOR = '80cbc4';
var LIGHT_DEFAULT_ACTIONBAR_COLOR = 'bdbdbd';
var LIGHT_DEFAULT_STATUSBAR_COLOR = '757575';
var LIGHT_DEFAULT_TITLE_COLOR = '000000';
var LIGHT_DEFAULT_ACCENT_COLOR = '009688';
function showHelp() {
console.log('Usage: generate-theme.js (Dark|Light|Light.DarkActionBar) actionBarColor statusBarColor titleColor accentColor ');
console.log('Example: generate-theme.js Light dcdcdc 757575 000000 ff0000');
console.log('Colors can be blank for default');
process.exit(1);
}
if (process.argv.length != 7) {
showHelp();
}
var styleFile = require('path').join(__dirname, 'app/src/main/res/values/styles.xml');
var colorFile = require('path').join(__dirname, 'app/src/main/res/values/colors.xml');
var theme = process.argv[2].toLowerCase();
var actionBarColor = process.argv[3].toLowerCase();
var statusBarColor = process.argv[4].toLowerCase();
var titleColor = process.argv[5].toLowerCase();
var accentColor = process.argv[6].toLowerCase();
var defaultActionBarColor;
var defaultStatusBarColor;
var defaultTitleColor;
var defaultAccentColor;
// verify inputs
if (theme === 'light') {
defaultActionBarColor = LIGHT_DEFAULT_ACTIONBAR_COLOR;
defaultStatusBarColor = LIGHT_DEFAULT_STATUSBAR_COLOR;
defaultTitleColor = LIGHT_DEFAULT_TITLE_COLOR;
defaultAccentColor = LIGHT_DEFAULT_ACCENT_COLOR;
} else if (theme === 'dark' || theme === 'light.darkactionbar') {
defaultActionBarColor = DARK_DEFAULT_ACTIONBAR_COLOR;
defaultStatusBarColor = DARK_DEFAULT_STATUSBAR_COLOR;
defaultTitleColor = DARK_DEFAULT_TITLE_COLOR;
defaultAccentColor = DARK_DEFAULT_ACCENT_COLOR;
} else {
console.log('Invalid theme ' + theme);
showHelp();
}
if (actionBarColor !== '' && !/^([0-9a-f]){6}$/.test(actionBarColor)) {
console.log('Invalid action bar background color ' + actionBarColor);
showHelp();
}
if (statusBarColor !== '' && !/^([0-9a-f]){6}$/.test(statusBarColor)) {
console.log('Invalid status bar color color ' + statusBarColor);
showHelp();
}
if (titleColor !== '' && !/^([0-9a-f]){6}$/.test(titleColor)) {
console.log('Invalid action bar title color ' + titleColor);
showHelp();
}
if (accentColor !== '' && !/^([0-9a-f]){6}$/.test(accentColor)) {
console.log('Invalid accent color ' + accentColor);
showHelp();
}
// set theme
if (theme === 'light') {
console.log('Setting light theme');
replaceStringInFile(styleFile, '<style name="GoNativeTheme.WithActionBar" parent="GN.LightWithDarkActionBar">',
'<style name="GoNativeTheme.WithActionBar" parent="GN.Light">');
} else if (theme === 'dark') {
console.log('Setting dark theme');
replaceStringInFile(styleFile, '<style name="GoNativeTheme.WithActionBar" parent="GN.LightWithDarkActionBar">',
'<style name="GoNativeTheme.WithActionBar" parent="GN.Dark">');
} else if (theme === 'light.darkactionbar') {
console.log('Setting light with dark actionbar theme');
// default theme, no change necessary
}
// set actionbar background color
if (actionBarColor !== '' && actionBarColor !== defaultActionBarColor) {
console.log('Setting action bar background color to ' + actionBarColor);
var s = '<item name="colorPrimary">#' + actionBarColor + '</item>';
replaceStringInFile(styleFile, '<!--GoNative placeholder: colorPrimary-->', s);
}
// set status bar color
if (statusBarColor !== '' && statusBarColor !== defaultStatusBarColor) {
console.log('Setting status bar background color to ' + statusBarColor);
var s = '<item name="colorPrimaryDark">#' + statusBarColor + '</item>';
replaceStringInFile(styleFile, '<!--GoNative placeholder: colorPrimaryDark-->', s);
}
// set actionbar title color
if (titleColor !== '' && titleColor !== defaultTitleColor) {
console.log('Setting action bar title color to ' + titleColor);
var s = '<item name="android:textColorPrimary">#' + titleColor + '</item>';
replaceStringInFile(styleFile, '<!--GoNative placeholder: titleTextColor-->', s);
}
// tab foreground color
var tabForegroundColor = titleColor == '' ? defaultTitleColor : titleColor;
console.log('Setting tab foreground color to ' + tabForegroundColor);
replaceStringInFile(colorFile, '<color name="tab_foreground_color">#FFFFFF</color>',
'<color name="tab_foreground_color">#' + tabForegroundColor +'</color>');
// set accent color
if (accentColor !== '' && accentColor !== defaultAccentColor) {
console.log('Setting accent color to ' + accentColor);
var s = '<item name="colorAccent">#' + accentColor + '</item>';
replaceStringInFile(styleFile, '<!--GoNative placeholder: colorAccent-->', s);
}
function replaceStringInFile(filename, searchvalue, newvalue) {
var fs = require('fs');
contents = fs.readFileSync(filename, {
encoding: 'utf8'
});
var newContents = contents.replace(searchvalue, newvalue);
fs.writeFileSync(filename, newContents);
}