Automating The App Creation Process using Python .
Hi there,
Nothing beats the thrill of completing or maintaning a project than starting out a new project especially when your using your favourite tools.
However, overtime the process gets a tad monotonous especially when you hit third or fourth project. Majority of Software Engineers or DEVs , tend to have multiple stacks hence the process of starting apps gets repetitive uncless you switch operating systems often . Hence the necessity of AUTOMATION
My thought process when starting an new Project( PS: I use Linux ):
Key note:
I store all my software projects under the Documents Directory whereby for :
- Mobile Projects I store them under : Documents/Mobile Development
- Web Projects I store them under : Documents/Web Development
- Hardware Projects I store them under : Documents/Hardware Development
- Scripts I store them under : Documents/Scripts
My Process :
- Change Directory :
cd Documents
2. Depending on whichever type of solution i am building I cd to the related directory :
cd Documents/Mobile Development or cd Documents/Web Development or cd Documents/Hardware Development
3. Create the new project app :
In my arsenal I posses skills in VueJS , Flutter, Android Native or Arduino depending on the project . Hence different approach when starting out each app. ( At the time of writing this )
- For vue JS i combine it with vite hence
npm init vite@latest
- Flutter
flutter create myapp
- For Native Android I use the Android Studio IDE itself, powerful one.
- For IoT Hardware related I use the Arduino IDE itself, one can also use other platfoms like platform.io via VS Code/VScodium.
4. Open Github, create a new private repository
5. Run the following git commands on terminal
git init
git add .
git commit -m "Hey there Champ this is your first commit"
git branch -M main
git remote add origin https://github.com/JosephRidge/myapp.git
git push -u origin main
6. Key in credentials ( github email and Personal Access Token )via terminal as prompted by git.
7. Voila ! Done .
When you hit step 5 is when it takes a toll on you hehehe….
The process above is tedious especially when you are in your journey to becoming a talented developer.
In this Piece we will AUTOMATE the whole process using Python .
Prerequisites
Steps
- Create folder , cd to the folder .
- Create two python files , secrets.py and createapp.py
- Launch Github on browser and create repository for it.
- On terminal run :
touch .gitignore && echo 'secrets.py' >> .gitignore
this command will allow you not to push your secrets.py file to git as it will contain your credentials.
5. Proceed with the respective git commands to push the project to github repo.
6. Proceed to Coding :
Part 1: secrets.py
Will contain your email address, passowrd and Personal Access Token ( i created one for each project typee.
myEmail = “myemailaddresss@gmail.com”myPassword = “myaccountpassword”webGithubPAT = “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”flutterGithubPAT = “ghp_bzfXomYNxfXRF2JzypReregr43grg7g3Z7zHR”hardwareGithubPAT = “ghp_gergergergc34tevrt5vhhj67j7jS0SwopD3YNJiu”nativeAndoridGithubPAT = "ghp_efwwegwqcq4t4t3434f3ypR7g3Z7zHR"
Part 2 : createapp.py
importations :
import osimport secrets # this contains your credentialsfrom selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.keys import Keysfrom webdriver_manager.chrome import ChromeDriverManagerfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC
Just a note, When I build software i tend to take a function based approach as opposed to putting everything in one whole main function. Each task / process is a function , this enables easy maintainenance
main() :
This function will serve the purpose of asking the user which type of platform he/she will be building if user selects a different option the process will start over . Kindly note that the indentation is not define here , however in python indentation is Key . ( Link to the Repo is at the end of this article )
def mainApp():print("\n** Let's Keep Building ! Great Work Jay R ! **** \n")startAppQuestion = input("Would you like to create an app ?(y/n)")if (startAppQuestion == "y" or startAppQuestion == "Y" or startAppQuestion =="yes"):print("\nYasssss lets do this ..... ")os.chdir("/home/ridge/Documents")print("\n_______________________________________\n")print("Available Platforms --> \n 1. Vue3 Web Application. \n 2. Flutter Application. \n 3. Android Application. \n 4. Hardware Project (Arduino/ Platfrom.io etc) ")choosePlatformToUse = input("\nUnder Which technology would you like to create your platform with ? (1,2,3 or 4) ")return choosePlatformToUseelse:print("Thank you for your time , bye! ")exit()
githubOperation() :
This function serves the purpose of launching chrome and opening github link. Afterwards it gets input fiels of both useremail and password and passes in your credentials.
Ass you can see to attain the credentials we saved in secrets.py we use secrets appended with a dot operator and the particular secret we hid. eg secrets.myPassword.
One thing you may realize and might confuse is how to attain the XPATH of a DOM element :
- navigate to any broswer page.
- right click on an element and click inspect it will open a panel either on your right or bottom
- On your left side click onto the highlighted arrow inside a box :
- once clicked select and item from the DOM / web page. This will highlight some code section within your inspect section. Hover your mouse over it and right click > copy > copy XPATH
def githubOperation(newGithubRepoName, githubPAT):print("setting up github .. ")driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))driver.get("https://github.com/login")myGithubEmail = driver.find_element(By.XPATH,'//*[@id="login_field"]')myGithubEmail.send_keys(secrets.myEmail)myGithubPass = driver.find_element(By.XPATH,'//*[@id="password"]')myGithubPass.send_keys(secrets.myPassword)myGithubButton = driver.find_element(By.XPATH,'//*[@id="login"]/div[4]/form/div/input[12]')myGithubButton.click()print(f"==== > URL === > {driver.current_url}")if(driver.current_url == "https://github.com/sessions/verified-device"):githubAccountVerfication(driver)print(" --- - -- - - - - -- - - > . > > .. > > ")driver.get("https://github.com/new")newGithubRepositoryName = driver.find_element(By.XPATH, '//*[@id="repository_name"]')newGithubRepositoryName.send_keys(newGithubRepoName)repositoryAccessType = driver.find_element(By.XPATH,'//*[@id="repository_visibility_private"]')repositoryAccessType.click()createGithubRepo = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="new_repository"]/div[6]/button')))createGithubRepo.click()remoteRepoUrl = driver.find_element(By.XPATH, '//*[@id="empty-setup-new-repo-echo"]/span[6]/span')print(f" --- > \n\n {remoteRepoUrl.text}\n\n")os.system("git init")os.system("git add .")os.system("git commit -m 'A Step To Greatness Hence, the First Commit'")os.system("git branch -M main")os.system(f"git remote add origin {remoteRepoUrl.text}")print(f"\n {githubPAT} \n")os.system(f"git push -u origin main ")
createApp():
I used the same thought process in starting an app from terminal and implemented for VueJS, Flutter and Arduino. A common operation in all i s that the must be stored in their selective project type directive eg if FLutter → Mobile Development, VueJS → Web Development and hencforth.
A sample of one of them is :
def createVue3App():# secrets.webGithubPAT = "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"print("\n........................................\n")os.chdir("/home/ridge/Documents/Web Development")print(os.getcwd())print("npm init vite@latest is running ........")os.system("npm init vite@latest")projectName = input("\nkindly input your new project name : ( the one you set as your new vuew app) : ")os.chdir(f"/home/ridge/Documents/Web Development/{projectName}")print(os.getcwd())githubOperation(projectName,secrets.webGithubPAT)print("Vue3 App created and updated to the github Repo !! ")print("\n........................................\n")
selectedItem(item):
Checks the type of application the user has selected if the user selects a choice outside the defines selections it returns “0” and the selection process starts all over :
def selectedItem(item):print(f"Item selected was : {item}")if item == "1":createVue3App()elif item == "2":createFlutterApp()elif item == "3":createAndroidApp()elif item == "4":createHardwareProject()else:print("Invalid Selection .. Select valid choice of either 1,2,3 or 4...\n")return "0"
Calling the main() function :
if (selectedItem(mainApp()) == "0"):selectedItem(mainApp())else:print(f"\n Thank you {name}\n"
To run the app you need to cd to the directory that you created the python script at and run using python createapp.py
Running Locally :
updated on github :
Considerations taken :
There exist Instances whereby github request OTP input from user, hence teh script wil check the current url after inputing password and email afterwards if redirected to OTP page we request user to input otp on terminal and proceed with operation else we proceed as to repo creation . This is handled by :
githubAccountVerfication(driver):
def githubAccountVerfication(driver):int("\n**************** ENTER GITHUB OTP ********************\n")githubOTPCode = driver.find_element(By.XPATH, '//*[@id="otp"]')if(githubOTPCode != None):gitOtp = input("Input your OTP code : ")githubOTPCode.send_keys(gitOtp)githubOTPButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="login"]/div[3]/form/button')))githubOTPButton.click()else:print("Proceeding to github repo creation ....")print("\n**********************************************************\n")
Thank you for reading all the way apologies it has been quite long however, if your would like to collaborate and improve this build feel free to raise an issue on Github ill be happy to improve and share to other software engineers .
Here is the link to the GitHub Repo.