Skip to content

Instantly share code, notes, and snippets.

@christianascone
Last active February 25, 2020 11:03
Show Gist options
  • Save christianascone/9147d3f60ce41ff7f54d5cf0d0d34ad3 to your computer and use it in GitHub Desktop.
Save christianascone/9147d3f60ce41ff7f54d5cf0d0d34ad3 to your computer and use it in GitHub Desktop.
React Native: Remove unused styles from external file
import re
import sys
from argparse import ArgumentParser
"""
A script to check a React Native view class and its stylesheet external file, removing unused style classes.
It search occurrences of given variable name (which must be the name of imported stylesheet class), comparing usages with
stylesheet class.
It eventually removes unused style classes from style file.
"""
def read_view_file(view_path, variable):
"""Read the view class file checking usages of style defined as 'variable'
Args:
view_path (TYPE): Path of file with view class
variable (TYPE): Name of imported variable of stylesheet class
Returns:
TYPE: A list of found classes
"""
found = []
with open(view_path, 'r') as j:
line = j.readline()
while line:
m = re.findall(variable + '\.(\w+)', line)
if m:
for match in m:
found.append(match)
line = j.readline()
print(found)
print('\n\n')
return found
def process_style_file(style_path, found):
"""Process the stylesheet class removing style classes not found in 'found' list
Args:
style_path (TYPE): Path of file with stylesheet class
found (TYPE): List of classes used by view class
Returns:
TYPE: Updated content of stylesheet class without unused style classes
"""
style_content = ""
with open(style_path, 'r') as j:
line = j.readline()
start = False
include = True
while line:
if start:
m = re.search('^ },', line)
if m is not None:
start = False
else:
m = re.search('^ (\w+): {', line)
if m is not None:
start = True
match = m.group(1)
if match not in found:
include = False
else:
include = True
elif not include:
include = True
if include:
style_content = style_content + line
line = j.readline()
print(style_content)
return style_content
def main(arguments):
parser = ArgumentParser()
parser.add_argument("-sf", "--stylefile", dest="stylefile",
help="Style File path.", metavar="stylefile")
parser.add_argument("-vf", "--viewfile", dest="viewfile",
help="View File path.", metavar="viewfile")
parser.add_argument("-v", "--variable", dest="variable",
help="Style variable.", metavar="variable")
args = parser.parse_args()
style_path = args.stylefile
if not style_path:
style_path = input('Enter style file path: ')
view_path = args.viewfile
if not view_path:
view_path = input('Enter view file path: ')
variable = args.variable
if not variable:
variable = input('Enter style variable: ')
found = read_view_file(view_path, variable)
style_content = process_style_file(style_path, found)
overwrite = input('Do you want to overwrite style file? [y/n] ')
if overwrite.lower() == 'yes' or overwrite.lower() == 'y':
with open(style_path, 'w') as f:
f.write(style_content)
print("File written")
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
/*
* Copyright (C) Christian Ascone - All Rights Reserved
*
* @file LoginScreen.tsx
* @author Christian Ascone
* @date 12/27/19 10:02 AM
*/
import React, {Component} from 'react';
import {ScrollView, TouchableOpacity, View, Text} from 'react-native';
import {i18n} from "@i18n/i18n";
import exampleClassScreenStyle from "@styles/ExampleClassScreenStyle";
import globalScreenStyle from "@styles/GlobalStyle";
export default class ExampleClassScreen extends Component<any, any> {
static navigationOptions = {};
constructor(props) {
super(props);
}
render() {
return (
<View style={exampleClassScreenStyle.container}>
<ScrollView
style={exampleClassScreenStyle.container}
contentContainerStyle={exampleClassScreenStyle.contentContainer}>
<View
style={[exampleClassScreenStyle.getStartedContainer, exampleClassScreenStyle.container, globalScreenStyle.globalMargins]}>
<View style={exampleClassScreenStyle.welcomeContainer}>
</View>
<View style={exampleClassScreenStyle.inputContainer}>
</View>
<View style={exampleClassScreenStyle.buttonContainer}>
</View>
<TouchableOpacity style={exampleClassScreenStyle.buttonContainer}
onPress={() => consle('Forgot')}>
<Text
style={exampleClassScreenStyle.forgotPassword}>Forgot</Text>
</TouchableOpacity>
</View>
</ScrollView>
</View>
);
}
}
/*
* Copyright (C) Christian Ascone - All Rights Reserved
*
* @file LoginScreenStyle.ts
* @author Christian Ascone
* @date 9/17/19 3:27 PM
*/
import {Platform, StyleSheet} from "react-native";
import Colors from "../config/Colors";
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
forgotPassword: {
textAlign: 'right',
},
developmentModeText: {
marginBottom: 20,
color: 'rgba(0,0,0,0.4)',
fontSize: 14,
lineHeight: 19,
textAlign: 'center',
},
contentContainer: {
justifyContent: 'space-between',
paddingTop: 30,
},
welcomeContainer: {
flex: 1,
marginTop: 10,
marginBottom: 50,
},
inputContainer: {
flex: 2,
marginTop: 10,
marginBottom: 20,
},
buttonContainer: {
flex: 2,
marginTop: 10,
marginBottom: 20,
},
welcomeImage: {
width: 150,
height: 50,
resizeMode: 'contain',
marginTop: 3,
},
welcomeText: {
flex: 1,
},
getStartedContainer: {
height: '100%',
},
dashboardReservationsScreenFilename: {
marginVertical: 7,
},
codeHighlightText: {
color: 'rgba(96,100,109, 0.8)',
},
codeHighlightContainer: {
backgroundColor: 'rgba(0,0,0,0.05)',
borderRadius: 3,
paddingHorizontal: 4,
},
getStartedText: {
fontSize: 17,
color: Colors.textColor,
lineHeight: 24,
textAlign: 'center',
},
tabBarInfoContainer: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
...Platform.select({
ios: {
shadowColor: 'black',
shadowOffset: {width: 0, height: -3},
shadowOpacity: 0.1,
shadowRadius: 3,
},
android: {
elevation: 20,
},
}),
alignItems: 'center',
backgroundColor: '#fbfbfb',
paddingVertical: 20,
},
tabBarInfoText: {
fontSize: 17,
color: Colors.textColor,
textAlign: 'center',
},
navigationFilename: {
marginTop: 5,
},
helpContainer: {
marginTop: 15,
alignItems: 'center',
},
helpLink: {
paddingVertical: 15,
},
helpLinkText: {
fontSize: 14,
color: '#2e78b7',
},
});
export default styles;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment