Skip to content

Instantly share code, notes, and snippets.

@travisbhartwell
Created December 23, 2024 01:42
Show Gist options
  • Save travisbhartwell/6d0a0bd237eb710af680d3be88807f40 to your computer and use it in GitHub Desktop.
Save travisbhartwell/6d0a0bd237eb710af680d3be88807f40 to your computer and use it in GitHub Desktop.
myproject file for all of my local myproject files
#!/usr/bin/env -S mycmd project run
# -*- mode: shell-script; sh-shell: bash; sh-basic-offset: 4; sh-indentation: 4; coding: utf-8 -*-
# shellcheck shell=bash
set -o nounset -o errexit -o errtrace -o pipefail
project.load_task_library "shell"
# shellcheck disable=SC2154
readonly ARCHIVE_DIRECTORY="${MYPROJECT_BASE_DIR}/archive"
if ! [[ -d "${ARCHIVE_DIRECTORY}" ]]; then
mkdir -p "${ARCHIVE_DIRECTORY}" || true
fi
mycmd.trace "Set the following variables:"
mycmd.trace "- ARCHIVE_DIRECTORY: ${ARCHIVE_DIRECTORY}"
#----------------------------------------
# Project File Sets
#----------------------------------------
# Just myproject
project.register_fileset MYPROJECT_ONLY
# shellcheck disable=SC2154
project.add_files_to_fileset MYPROJECT_ONLY "${MYPROJECT_PROJECT_FILE}"
project.register_task_with_fileset list-myproject-only project.list-files MYPROJECT_ONLY
project.register_task_with_fileset format-myproject-only project:shell.format MYPROJECT_ONLY
project.register_task_with_fileset lint-myproject-only project:shell.lint MYPROJECT_ONLY
#----------------------------------------
# All archived files
project.register_fileset ALL_ARCHIVED_FILES
mycmd.init_bin find
function get_archived_files() {
local -a archived_files
archived_files=("${ARCHIVE_DIRECTORY}"/*)
printf '%q\n' "${archived_files[@]}"
}
mycmd.init_bin realpath
function add_archived_files_to_fileset() {
# Work around current limitations in find_files_for_fileset and add_files_to_fileset when working with symlinks
local -a archived_files
readarray -t archived_files < <(mycmd.nullglob_set_wrapper get_archived_files || sort)
local f
local relative_f
for f in "${archived_files[@]}"; do
if ! relative_f="$(mycmd.bin_execute realpath --no-symlinks --relative-to="${MYPROJECT_BASE_DIR}" "${f}")"; then
mycmd.debug "Error getting relative path for '${f}'."
fi
ALL_ARCHIVED_FILES+=("${relative_f}")
done
}
add_archived_files_to_fileset
# TODO: Remove when new snapshot has this feature in project.list-files
function list-files() {
local -n fileset="${1}"
printf '%s\n' "${fileset[@]}"
}
project.register_task_with_fileset list-all-archived-files \
list-files \
ALL_ARCHIVED_FILES
#----------------------------------------
# File Archive Tasks
function get_archive_file_name() {
local -r myproject_file="${1}"
local project_name
project_name="$(basename "$(dirname "${myproject_file}")")"
readonly project_name
echo "${ARCHIVE_DIRECTORY}/${project_name}"
}
function gather-myproject-files() {
local -A archive_files=()
local myproject_file
local archive_file_name
while IFS= read -r -d $'\0' myproject_file; do
if [[ -d "${myproject_file}" ]]; then
project.output_only_if_not_quiet "Skipping '${myproject_file}', as it is a directory."
continue
fi
if [[ "${myproject_file}" =~ (mycmd-old|snapshot) ]]; then
project.output_only_if_not_quiet "Skipping archive and snapshot checkout at '${myproject_file}'."
continue
fi
project.output_only_if_not_quiet "Examining myproject file at '${myproject_file}'."
archive_file_name=$(get_archive_file_name "${myproject_file}")
if [[ -v archive_files["${archive_file_name}"] ]]; then
local existing_file="${archive_files["${archive_file_name}"]}"
mycmd.output "Found conflicting file for using archive file name '${archive_file_name}' for '${myproject_file}'."
mycmd.output "It is already defined for '${existing_file}'."
continue
fi
archive_files["${archive_file_name}"]="${myproject_file}"
ln -sf "${myproject_file}" "${archive_file_name}"
done < <(mdfind -0 -literal 'kMDItemFSName = "myproject"')
}
project.register_task gather-myproject-files
#----------------------------------------
# Analysis of all files
function count_source_lines() {
local -n fileset="${1}"
project.output_only_if_not_quiet "Counting source lines in fileset '${!fileset}'."
wc -l "${fileset[@]}" | sort -nr
}
project.register_task_with_fileset count-all-archived-file-source-lines \
count_source_lines \
ALL_ARCHIVED_FILES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment