diff --git a/.gitignore b/.gitignore index ed99f9be..52adfe39 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ prelude.output.md # Scratch files *.u + +# Developers can customize their own local development setup +/transcripts/fixtures/custom_projects.txt diff --git a/Makefile b/Makefile index 4d2256b2..6e395e1f 100644 --- a/Makefile +++ b/Makefile @@ -71,8 +71,8 @@ reset_fixtures: sleep 1; \ done; @echo "Booting up share"; - @( . ./local.env \ - $(exe) 2>&1 & \ + @( . ./local.env ; \ + $(exe) & \ SERVER_PID=$$!; \ trap "kill $$SERVER_PID 2>/dev/null || true" EXIT INT TERM; \ echo "Loading fixtures"; \ diff --git a/README.md b/README.md index 7e1b1a44..3dd07fb6 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ A [flake.nix](flake.nix) file is provided in this repo. It currently doesn't use ## Running Locally The first time you run locally, start with `make reset_fixtures`, then on subsequent runs just use `make serve`. +`make reset_fixtures` will set up some basic test data in your local database, and will copy all projects from `./transcripts/fixtures/projects.txt` and `./transcripts/fixtures/custom_projects.txt` from Share into your local database. Data changes in Postgres using `make serve` are persistent locally. You can reset the database to a known state with `make reset_fixtures`. diff --git a/transcripts/fixtures/load-from-share.sh b/transcripts/fixtures/load-from-share.sh index a8124b2c..fd0b9c76 100755 --- a/transcripts/fixtures/load-from-share.sh +++ b/transcripts/fixtures/load-from-share.sh @@ -5,9 +5,6 @@ set -e set -u -echo "This is not yet implemented" -exit 1 - . "${SHARE_PROJECT_ROOT}/transcripts/transcript_functions.sh" cache_dir="$HOME/.cache/share-api" @@ -16,44 +13,49 @@ if [ ! -d "$cache_dir" ]; then mkdir -p "$cache_dir" fi -typeset -A projects -projects=( - base '@unison/base' -) - -for project_name project_ref in "${(@kv)projects}"; do - echo "Downloading sync file for $project_ref" - output_file="$(mktemp)" - curl -X GET --location "https://api.unison-lang.org/ucm/v1/projects/project?name=${project_ref}" \ - --header 'Content-Type: application/json' \ - >"$output_file" - - echo "Response for project $project_ref:" - cat "$output_file" - - latest_release="$(jq -r '.payload."latest-release"' <"$output_file")" - projectId="$(jq -r '.payload."project-id"' <"$output_file")" - branch_ref="releases/${latest_release}" - project_branch_ref="${project_ref}/${branch_ref}" - - curl -X GET --location "https://api.unison-lang.org/ucm/v1/projects/project-branch?projectId=${projectId}&branchName=releases/${latest_release}" \ - --header 'Content-Type: application/json' \ - >"$output_file" - branch_head="$(jq -r '.payload."branch-head"' <"$output_file")" - - echo "Response for project branch $project_branch_ref:" - cat "$output_file" - - sync_file="$cache_dir/${project_branch_ref}" - - if [ -f "$sync_file" ]; then - echo "Sync file for $project_branch_ref already exists, skipping download." - continue - else - mkdir -p "$(dirname "$sync_file")" - echo "Downloading sync file for $project_branch_ref into $sync_file" - curl -X POST --location 'https://api.unison-lang.org/ucm/v2/sync/entities/download' \ - --header 'Content-Type: application/json' \ - --data-raw "{\"branchRef\": \"${project_branch_ref}\", \"causalHash\": \"${branch_head}\", \"knownHashes\":[]}" >"$cache_dir/${project_branch_ref}" - fi -done +main_project_list="${SHARE_PROJECT_ROOT}/transcripts/fixtures/projects.txt" +custom_project_list="${SHARE_PROJECT_ROOT}/transcripts/fixtures/custom_projects.txt" +# create the custom projects file if it doesn't exist +if [ ! -f "$custom_project_list" ]; then + touch "$custom_project_list" +fi + +auth_transcript="$(mktemp).md" +pull_transcript="$(mktemp).md" +push_transcript="$(mktemp).md" + +cat << EOF >"$auth_transcript" +\`\`\`ucm +scratch/main> auth.login +\`\`\` +EOF + +while IFS= read -r line; do + read -r project_source project_dest <<< "$line" +# Annoyingly clone will fail if it's already been cloned, but succeed otherwise, so we just add a bad command to +# force the block to always fail so we can use the :error directive. +cat << EOF +\`\`\`ucm:error +scratch/main> clone ${project_source} +scratch/main> force-failure +\`\`\` + +EOF +done < <(cat "$main_project_list" "$custom_project_list") >"$pull_transcript" + +while IFS= read -r line; do + read -r project_source project_dest <<< "$line" +cat << EOF +\`\`\`ucm +${project_source}> push ${project_dest} +\`\`\` + +EOF +done < <(cat "$main_project_list" "$custom_project_list") >"$push_transcript" + +echo "📫 Downloading projects from Share" +UNISON_SHARE_HOST="https://api.unison-lang.org" ucm -C "${cache_dir}/code-cache" transcript.in-place "$pull_transcript" +echo "🔑 Authenticating with local server..." +UNISON_SHARE_HOST="http://localhost:5424" ucm -c "${cache_dir}/code-cache" transcript.in-place "$auth_transcript" +echo "📦 Pushing projects to local server..." +UNISON_SHARE_HOST="http://localhost:5424" ucm -c "${cache_dir}/code-cache" transcript.in-place "$push_transcript" diff --git a/transcripts/fixtures/projects.txt b/transcripts/fixtures/projects.txt new file mode 100644 index 00000000..8ce9a417 --- /dev/null +++ b/transcripts/fixtures/projects.txt @@ -0,0 +1 @@ +@unison/base/releases/4.8.0 @test/base/main diff --git a/transcripts/fixtures/run.zsh b/transcripts/fixtures/run.zsh index e6b331e4..38d257ed 100755 --- a/transcripts/fixtures/run.zsh +++ b/transcripts/fixtures/run.zsh @@ -6,3 +6,5 @@ source "${SHARE_PROJECT_ROOT}/transcripts/transcript_functions.sh" # Set up database so helper scripts know it's a local db pg_file "${SHARE_PROJECT_ROOT}/transcripts/sql/configure_local_database.sql" pg_init_fixtures + +source "${SHARE_PROJECT_ROOT}/transcripts/fixtures/load-from-share.sh"