Skip to content

Commit

Permalink
feat: add for macOS and linux (Ubuntu)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoli0605 committed Mar 12, 2024
1 parent 3804e08 commit 4a436aa
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 28 deletions.
24 changes: 21 additions & 3 deletions packageData.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@
"install": {
"linux": "asdf plugin add nodejs && asdf install nodejs 16.20.2 && asdf global nodejs 16.20.2",
"mac": "asdf plugin add nodejs && asdf install nodejs 18.18.0 && asdf global nodejs 18.18.0",
"windows": "choco install nvm -y; nvm install 18.18.0; nvm use 18.18.0"
"windows": "choco install nvm -y ; nvm install 18.18.0 ; nvm use 18.18.0"
},
"type": "force"
},
"OpenSSH": {
"description": "",
"install": {
"linux": "sudo apt install openssh-server -y",
"linux": "sudo apt install openssh-server -y && mkdir -p ~/.ssh && chmod 700 ~/.ssh && touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys",
"mac": "",
"windows": ""
},
Expand Down Expand Up @@ -234,6 +234,24 @@
},
"type": "disable"
},
"TypeScript": {
"description": "",
"install": {
"linux": "npm install -g typescript ts-node",
"mac": "npm install -g typescript ts-node",
"windows": "npm install -g typescript ts-node"
},
"type": "enable"
},
"VSCode Extension Development": {
"description": "VSCode 擴充套件開發工具",
"install": {
"linux": "npm install -g yo generator-code vsce esbuild",
"mac": "npm install -g yo generator-code vsce esbuild",
"windows": "npm install -g yo generator-code vsce esbuild"
},
"type": "disable"
},
"Virtual Box": {
"description": "自由及開放原始碼的虛擬機器軟體",
"install": {
Expand Down Expand Up @@ -510,7 +528,7 @@
"description": "",
"install": {
"linux": "",
"mac": "brew install --cask microsoft-edge",
"mac": "brew install --cask microsoft-edge && echo 'export CHROME_EXECUTABLE=\"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge\"' >>~/.zshrc",
"windows": ""
},
"type": "enable"
Expand Down
Empty file removed scripts/linux/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions scripts/linux/asdf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0
echo '. "$HOME/.asdf/asdf.sh"' >>~/.bashrc
echo '. "$HOME/.asdf/completions/asdf.bash"' >>~/.bashrc
source ~/.bashrc
17 changes: 17 additions & 0 deletions scripts/linux/basesetup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

sudo apt install -y curl software-properties-common apt-transport-https lsb-release
sudo apt update && sudo apt upgrade -y

# Load git-prompt.sh to use __git_ps1 for Git branch name
echo "# Load git prompt script for version control information" >> ~/.bashrc
echo "if type __git_ps1 &>/dev/null; then" >> ~/.bashrc
echo " GIT_PS1_SHOWDIRTYSTATE=1" >> ~/.bashrc
echo " GIT_PS1_SHOWSTASHSTATE=1" >> ~/.bashrc
echo " GIT_PS1_SHOWUNTRACKEDFILES=1" >> ~/.bashrc
echo " GIT_PS1_SHOWUPSTREAM='auto'" >> ~/.bashrc
echo " PROMPT_COMMAND='__git_ps1 \"\\[\\e[32m\\]\\w\\[\\e[0m\\]\" \" \\\$ \" \""'"'%F{87}(%s)%f'"'"'\"'" >> ~/.bashrc
echo "else" >> ~/.bashrc
echo " PS1='\\[\\e[32m\\]\\w\\[\\e[0m\\] \$ '" >> ~/.bashrc
echo "fi" >> ~/.bashrc
echo "" >> ~/.bashrc
5 changes: 5 additions & 0 deletions scripts/linux/poetry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

curl -sSL https://install.python-poetry.org | python3
echo 'export PATH="$HOME/.local/bin:$PATH"' >>~/.bashrc
source ~/.bashrc
Empty file removed scripts/macos/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions scripts/macos/asdf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

brew install asdf
echo ". /opt/homebrew/opt/asdf/libexec/asdf.sh" >>~/.zshrc
source ~/.zshrc
9 changes: 9 additions & 0 deletions scripts/macos/homebrew.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(
echo
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"'
) >>~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
source ~/.zprofile
14 changes: 14 additions & 0 deletions scripts/macos/macsetup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Reset Launchpad
# //////////////////////////////////////////////////
defaults write com.apple.dock ResetLaunchPad -bool true
killall Dock

# Display full path in Finder title bar
# //////////////////////////////////////////////////
defaults write com.apple.finder _FXShowPosixPathInTitle -bool true

# Show path bar in Finder
# //////////////////////////////////////////////////
defaults write com.apple.finder ShowPathbar -bool true
5 changes: 5 additions & 0 deletions scripts/macos/poetry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

curl -sSL https://install.python-poetry.org | python3
echo 'export PATH="$HOME/.local/bin:$PATH"' >>~/.zshrc
source ~/.zshrc
14 changes: 14 additions & 0 deletions scripts/macos/zshsetup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

echo "# Load version control information" >>~/.zshrc
echo "autoload -Uz vcs_info" >>~/.zshrc
echo "precmd() { vcs_info }" >>~/.zshrc
echo "" >>~/.zshrc
echo "# Format the vcs_info_msg_0_ variable" >>~/.zshrc
echo "zstyle ':vcs_info:git:*' formats ' %F{87}(%b)%f'" >>~/.zshrc
echo "" >>~/.zshrc
echo "# Set up the prompt (with git branch name)" >>~/.zshrc
echo "setopt PROMPT_SUBST" >>~/.zshrc
echo "PROMPT='[%F{82}%~%f]\${vcs_info_msg_0_} $ '" >>~/.zshrc
echo "" >>~/.zshrc
source ~/.zshrc
2 changes: 1 addition & 1 deletion scripts/npm.setup
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
npm install -g npm
npm install -g npm@latest
npm install -g pnpm
npm install -g npm-check-updates
npm install -g @leoli0605/git-setup
Empty file removed scripts/windows/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions scripts/windows/PoetryInstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
25 changes: 25 additions & 0 deletions src/bash.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import { getAppDir } from './dirname.mjs';
import ShellScript from './shell_script.mjs';

const __dirname = getAppDir();

class Bash extends ShellScript {
constructor() {
super();
this.scripts.push('#!/bin/bash\n');
}

getScript() {
const paths = this.environment.map((e) => `"${e}"`).join(',\n ');
let envSetup = fs.readFileSync(path.join(__dirname, '/scripts/windows/EnvSetup.ps1'), 'utf8');
envSetup = envSetup.replace('${PATHS}', paths);
this.scripts.push(envSetup + '\n');
this.scripts.push(this.commands.join('\n'));
return this.scripts.toString().replace(/(^,)/gm, '') + '\n';
}
}

export default Bash;
102 changes: 80 additions & 22 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import os from 'os';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import path from 'path';
import { cmd } from './cmd_process.mjs';
import { getAppDir } from './dirname.mjs';
import { selectPackages } from './package_selector.mjs';
Expand All @@ -11,63 +10,122 @@ const __dirname = getAppDir();

selectPackages()
.then((selectedPackages) => {
let shell;
let command = '';
let args = [];
if (os.platform() === 'win32') {
const ps = new PowerShell();
console.log('Windows detected');
shell = new PowerShell();

for (const p of selectedPackages) {
if (p) {
ps.addCommand(p.installCommand);
shell.addCommand(p.installCommand);
}
}
ps.addCommand('refreshenv\n'); // for powershell to refresh environment variables
shell.addCommand('refreshenv\n'); // for powershell to refresh environment variables

if (selectedPackages.some((p) => p && p.packageName.startsWith('Python'))) {
ps.addEnvironment('$HOME\\AppData\\Roaming\\Python\\Scripts');
ps.addCommand('python.exe -m pip install --upgrade pip'); // for pip
ps.addCommand('(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -'); // for poetry
ps.addCommand('poetry config virtualenvs.in-project true'); // for poetry
shell.addEnvironment('$HOME\\AppData\\Roaming\\Python\\Scripts');
const poetryInstall = fs.readFileSync(path.join(__dirname, 'scripts/windows/PoetryInstall.ps1'), 'utf8').trim();
shell.addCommand(poetryInstall);
shell.addCommand('poetry config virtualenvs.in-project true'); // for poetry
const pipSetup = fs.readFileSync(path.join(__dirname, 'scripts/pip.setup'), 'utf8').trim();
ps.addCommand(pipSetup);
shell.addCommand(pipSetup);
}
if (selectedPackages.some((p) => p && p.packageName.startsWith('Rust'))) {
ps.addEnvironment('$HOME\\leoli\\.cargo\\bin');
shell.addEnvironment('$HOME\\leoli\\.cargo\\bin');
}
ps.addCommand('refreshenv\n'); // for powershell to refresh environment variables
shell.addCommand('refreshenv\n'); // for powershell to refresh environment variables

if (selectedPackages.some((p) => p && p.packageName.startsWith('Node.js'))) {
const npmSetup = fs.readFileSync(path.join(__dirname, 'scripts/npm.setup'), 'utf8').trim();
ps.addCommand(npmSetup);
ps.addCommand('npx @leoli0605/git-setup'); // for git
shell.addCommand(npmSetup);
}
ps.addCommand('refreshenv\n'); // for powershell to refresh environment variables
shell.addCommand('refreshenv\n'); // for powershell to refresh environment variables

if (selectedPackages.some((p) => p && p.packageName.startsWith('Sublime Text'))) {
let sublimeSetup = fs.readFileSync(path.join(__dirname, 'scripts/windows/SublimeSetup.ps1'), 'utf8').trim();
sublimeSetup = sublimeSetup.replace('${PATH}', path.join(__dirname, 'scripts/windows/Preferences.sublime-settings'));
ps.addCommand(sublimeSetup);
shell.addCommand(sublimeSetup);
}
ps.addCommand('refreshenv\n'); // for powershell to refresh environment variables

const psScript = ps.getScripts();
// fs.writeFileSync(path.join(__dirname, 'scripts.ps1.log'), psScript);
shell.addCommand('refreshenv\n'); // for powershell to refresh environment variables
shell.addCommand('npx @leoli0605/git-setup'); // for git
const script = shell.getScript();

const encodedPsScript = Buffer.from(psScript, 'utf16le').toString('base64');
const encodedScript = Buffer.from(script, 'utf16le').toString('base64');
command = 'powershell.exe';
args = ['-NoProfile', '-EncodedCommand', encodedPsScript];
args = ['-NoProfile', '-EncodedCommand', encodedScript];
} else if (os.platform() === 'darwin') {
console.log('MacOS detected');
shell = new Bash();

shell.addCommand(fs.readFileSync(path.join(__dirname, 'scripts/macos/homebrew.sh'), 'utf8').trim());
shell.addCommand(fs.readFileSync(path.join(__dirname, 'scripts/macos/asdf.sh'), 'utf8').trim());

for (const p of selectedPackages) {
if (p) {
shell.addCommand(p.installCommand);
}
}

if (selectedPackages.some((p) => p && p.packageName.startsWith('Python'))) {
const poetryInstall = fs.readFileSync(path.join(__dirname, 'scripts/macos/poetry.sh'), 'utf8').trim();
shell.addCommand(poetryInstall);
shell.addCommand('poetry config virtualenvs.in-project true'); // for poetry
const pipSetup = fs.readFileSync(path.join(__dirname, 'scripts/pip.setup'), 'utf8').trim();
shell.addCommand(pipSetup);
}
if (selectedPackages.some((p) => p && p.packageName.startsWith('Node.js'))) {
const npmSetup = fs.readFileSync(path.join(__dirname, 'scripts/npm.setup'), 'utf8').trim();
shell.addCommand(npmSetup);
}
shell.addCommand(fs.readFileSync(path.join(__dirname, 'scripts/macos/zshsetup.sh'), 'utf8').trim());
shell.addCommand(fs.readFileSync(path.join(__dirname, 'scripts/macos/macsetup.sh'), 'utf8').trim());
shell.addCommand('npx @leoli0605/git-setup'); // for git
const script = shell.getScript();

command = 'bash';
args = ['-c', script];
} else if (os.platform() === 'linux') {
const distro = os
.release()
.split('.')
.map((v) => parseInt(v));
console.log('Linux distribution version:', distro);
shell = new Bash();

shell.addCommand(fs.readFileSync(path.join(__dirname, 'scripts/linux/basesetup.sh'), 'utf8').trim());
shell.addCommand(fs.readFileSync(path.join(__dirname, 'scripts/linux/asdf.sh'), 'utf8').trim());

for (const p of selectedPackages) {
if (p) {
shell.addCommand(p.installCommand);
}
}
if (distro[0] >= 20 /* && distro[1] === 4 */) {
console.log('Ubuntu 20.04 detected');
shell.addCommand('asdf install nodejs 18.18.0');
shell.addCommand('asdf global nodejs 18.18.0');
} else {
console.log('Ubuntu 20.04 not detected');
}

if (selectedPackages.some((p) => p && p.packageName.startsWith('Python'))) {
const poetryInstall = fs.readFileSync(path.join(__dirname, 'scripts/linux/poetry.sh'), 'utf8').trim();
shell.addCommand(poetryInstall);
shell.addCommand('poetry config virtualenvs.in-project true'); // for poetry
const pipSetup = fs.readFileSync(path.join(__dirname, 'scripts/pip.setup'), 'utf8').trim();
shell.addCommand(pipSetup);
}
if (selectedPackages.some((p) => p && p.packageName.startsWith('Node.js'))) {
const npmSetup = fs.readFileSync(path.join(__dirname, 'scripts/npm.setup'), 'utf8').trim();
shell.addCommand(npmSetup);
}
shell.addCommand('npx @leoli0605/git-setup'); // for git
const script = shell.getScript();

command = 'bash';
args = ['-c', script];
}

console.log(`command: ${command}`);
Expand Down
2 changes: 1 addition & 1 deletion src/powershell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PowerShell extends ShellScript {
this.scripts.push(fs.readFileSync(path.join(__dirname, 'scripts/windows/Chocolatey.ps1'), 'utf8') + '\n');
}

getScripts() {
getScript() {
const paths = this.environment.map((e) => `"${e}"`).join(',\n ');
let envSetup = fs.readFileSync(path.join(__dirname, 'scripts/windows/EnvSetup.ps1'), 'utf8');
envSetup = envSetup.replace('${PATHS}', paths);
Expand Down
2 changes: 1 addition & 1 deletion src/shell_script.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ShellScript {
console.log(`Environment added: ${value}`);
}

getScripts() {
getScript() {
throw new Error('You have to implement the method getScripts!');
}
}
Expand Down

0 comments on commit 4a436aa

Please sign in to comment.