From fad4b8500ac426f3e93f61bc28b8358eee28f594 Mon Sep 17 00:00:00 2001 From: Pratik Tripathy Date: Wed, 11 Sep 2024 23:00:09 +0530 Subject: [PATCH] feat(setup-script): Remove unnecessary options. Add --install & --dry-run options. Start install script when opted for. --- README.md | 52 ++++++++++++++++++------ setup.sh | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 11 deletions(-) create mode 100755 setup.sh diff --git a/README.md b/README.md index 23d3600..9920342 100755 --- a/README.md +++ b/README.md @@ -1,21 +1,51 @@ -# dotfiles +# Dotfiles -Place to keep my .dotfiles, so I do not have to worry about migrating to another system. +1. Restore my dotfiles on any *nix OS. +2. Install required applications on any Linux OS. + +Dotfiles are symlinked from the repo to the required location. +That is, changes on repository get auto reflected on the system. ## Usage ```bash -$ bash bootstrap.sh -h +$ git clone https://github.com/pratiktri/dotfiles -Applies all settings stored in the script's directory to your home directory +$ bash setup.sh -h +Apply all settings stored in the script's directory to your home directory. -Usage: bootstrap.sh [-q|--quiet] [-l|--create-links] - -q, --quiet No screen outputs - -l, --create-links Creates soft-links to files in the current directory instead of copying them - -Example: bash ./bootstrap.sh -q --create-links +Usage: ./setup.sh [OPTION] +Options: + -h, --help Show this help message. + -d, --dry-run Simulate dotfile symlink without doing anything. + -i, --install Install programs listed on package-list-os & package-list-brew files. ``` -## Why `--create-links`? +## Installation -I have multiple Linux installations on my machine. Linking it from one place (this repository) keeps things tidy. Also, changes to dotfiles automatically get applied to all the distros. +Installation scripts are inside `scripts` directory. + +It reads 2 text files to gather lists of software to install: + +1. `package-list-os` - To install using OS package manager. +2. `package-list-brew` - To install using brew package manager. + +Any package not available are *skipped*. + +`install.sh` calls `install-os-packages.sh` and `install-brew-packages.sh`. Both can be executed separately. + +## Test + +1. Need to be inside this directory + + ```bash + cd scripts/test + ``` + +2. Change `Dockerfile` as required + +3. Run the script + + ```bash + ./run-test.sh + ``` diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..baf8287 --- /dev/null +++ b/setup.sh @@ -0,0 +1,115 @@ +#!/bin/sh + +INSTALL="n" +DRY_RUN="n" + +show_help() { + echo "Apply all settings stored in the script's directory to your home directory" + echo + echo "usage: $0 [option]" + echo "options:" + echo " -h, --help show this help message" + echo " -d, --dry-run Simulate dotfile linking without doing anything" + echo " -i, --install Install programs listed on package-list-os & package-list-brew files" +} + +place_dotfile_at_target_location() { + source_file_location="$1" + file_target_location="$2" + TS="$3" + + # To avoid over writing an existing dot-file, we rename them + # Appending the timestamp to file name + if [ -f "$file_target_location" ] || [ -L "$file_target_location" ]; then + [ "$DRY_RUN" = "y" ] && echo "mv ${file_target_location} ${file_target_location}_${TS}" && echo "Existing dotfile renamed to ${file_target_location}_${TS}" + [ "$DRY_RUN" = "n" ] && mv "$file_target_location" "${file_target_location}_${TS}" && echo "Existing setting renamed to ${file_target_location}_${TS}" + fi + + target_directory=$(dirname "$file_target_location") + if [ ! -d "$target_directory" ]; then + mkdir -p "$target_directory" && echo "Directory ${target_directory} created" + fi + + [ "$DRY_RUN" = "y" ] && echo "ln -sf ${source_file_location} ${target_directory}" + [ "$DRY_RUN" = "n" ] && ln -sf "$source_file_location" "$target_directory" && echo "Linked ${file_target_location}" +} + +parse_input() { + # TODO: Accept input to execute install.sh script + + while [ "${#}" -gt 0 ]; do + case $1 in + -i | --install) + INSTALL="y" + shift + ;; + -d | --dry-run) + DRY_RUN="y" + shift + ;; + -h | --help) + show_help + exit 0 + ;; + *) + show_help + exit 1 + ;; + esac + done +} + +setup_symlinks() { + case $(uname -a) in + Linux*) + # TODO: Make it less KDE-Neon specific and more general Linux + OS="kde-neon" + + [ "$XDG_CURRENT_DESKTOP" = "KDE" ] && OS="kde-neon" + ;; + Darwin*) + OS="macos" + ;; + *) + OS="UNSUPPORTED" + ;; + esac + + TS=$(date '+%d_%m_%Y-%H_%M_%S') + + # Switch inside dotfile repository directory + cd -P "$(dirname "$0")" || exit + + # Copy all files in "Common" dotfiles to $HOME directory ("~") + find "./common" -type f -not -path '*.DS_Store' -not -path '*.directory' | while read -r file; do + file_target_location="${HOME}${file#./common}" + source_file_location="${PWD}${file#.}" + place_dotfile_at_target_location "$source_file_location" "$file_target_location" "$TS" + done + + # Copy platform-specific files to $HOME directory ("~") + find "./${OS}" -type f -not -path '*.DS_Store' -not -path '*.directory' | while read -r file; do + file_target_location="${HOME}${file#./"${OS}"}" + source_file_location="${PWD}${file#.}" + place_dotfile_at_target_location "$source_file_location" "$file_target_location" "$TS" + done +} + +main() { + parse_input "$@" + + install_file_location="$( + cd -- "$(dirname "$0")" > /dev/null 2>&1 || exit + pwd -P + )/scripts" + + setup_symlinks + echo "$install_file_location" + + if [ "$INSTALL" = "y" ]; then + cd "$install_file_location" || exit + [ "$DRY_RUN" = "n" ] && "$install_file_location"/install.sh + fi +} + +main "$@"