Skip to content

Commit 8a56c15

Browse files
authored
Merge pull request #140 from unisoncomputing/cp/project-fixtures
Copy select projects from prod in `reset_fixtures`
2 parents 05d2e5f + 71e9919 commit 8a56c15

File tree

6 files changed

+55
-46
lines changed

6 files changed

+55
-46
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ prelude.output.md
2020

2121
# Scratch files
2222
*.u
23+
24+
# Developers can customize their own local development setup
25+
/transcripts/fixtures/custom_projects.txt

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ reset_fixtures:
7171
sleep 1; \
7272
done;
7373
@echo "Booting up share";
74-
@( . ./local.env \
75-
$(exe) 2>&1 & \
74+
@( . ./local.env ; \
75+
$(exe) & \
7676
SERVER_PID=$$!; \
7777
trap "kill $$SERVER_PID 2>/dev/null || true" EXIT INT TERM; \
7878
echo "Loading fixtures"; \

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ A [flake.nix](flake.nix) file is provided in this repo. It currently doesn't use
6262
## Running Locally
6363

6464
The first time you run locally, start with `make reset_fixtures`, then on subsequent runs just use `make serve`.
65+
`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.
6566

6667
Data changes in Postgres using `make serve` are persistent locally.
6768
You can reset the database to a known state with `make reset_fixtures`.

transcripts/fixtures/load-from-share.sh

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
set -e
66
set -u
77

8-
echo "This is not yet implemented"
9-
exit 1
10-
118
. "${SHARE_PROJECT_ROOT}/transcripts/transcript_functions.sh"
129

1310
cache_dir="$HOME/.cache/share-api"
@@ -16,44 +13,49 @@ if [ ! -d "$cache_dir" ]; then
1613
mkdir -p "$cache_dir"
1714
fi
1815

19-
typeset -A projects
20-
projects=(
21-
base '@unison/base'
22-
)
23-
24-
for project_name project_ref in "${(@kv)projects}"; do
25-
echo "Downloading sync file for $project_ref"
26-
output_file="$(mktemp)"
27-
curl -X GET --location "https://api.unison-lang.org/ucm/v1/projects/project?name=${project_ref}" \
28-
--header 'Content-Type: application/json' \
29-
>"$output_file"
30-
31-
echo "Response for project $project_ref:"
32-
cat "$output_file"
33-
34-
latest_release="$(jq -r '.payload."latest-release"' <"$output_file")"
35-
projectId="$(jq -r '.payload."project-id"' <"$output_file")"
36-
branch_ref="releases/${latest_release}"
37-
project_branch_ref="${project_ref}/${branch_ref}"
38-
39-
curl -X GET --location "https://api.unison-lang.org/ucm/v1/projects/project-branch?projectId=${projectId}&branchName=releases/${latest_release}" \
40-
--header 'Content-Type: application/json' \
41-
>"$output_file"
42-
branch_head="$(jq -r '.payload."branch-head"' <"$output_file")"
43-
44-
echo "Response for project branch $project_branch_ref:"
45-
cat "$output_file"
46-
47-
sync_file="$cache_dir/${project_branch_ref}"
48-
49-
if [ -f "$sync_file" ]; then
50-
echo "Sync file for $project_branch_ref already exists, skipping download."
51-
continue
52-
else
53-
mkdir -p "$(dirname "$sync_file")"
54-
echo "Downloading sync file for $project_branch_ref into $sync_file"
55-
curl -X POST --location 'https://api.unison-lang.org/ucm/v2/sync/entities/download' \
56-
--header 'Content-Type: application/json' \
57-
--data-raw "{\"branchRef\": \"${project_branch_ref}\", \"causalHash\": \"${branch_head}\", \"knownHashes\":[]}" >"$cache_dir/${project_branch_ref}"
58-
fi
59-
done
16+
main_project_list="${SHARE_PROJECT_ROOT}/transcripts/fixtures/projects.txt"
17+
custom_project_list="${SHARE_PROJECT_ROOT}/transcripts/fixtures/custom_projects.txt"
18+
# create the custom projects file if it doesn't exist
19+
if [ ! -f "$custom_project_list" ]; then
20+
touch "$custom_project_list"
21+
fi
22+
23+
auth_transcript="$(mktemp).md"
24+
pull_transcript="$(mktemp).md"
25+
push_transcript="$(mktemp).md"
26+
27+
cat << EOF >"$auth_transcript"
28+
\`\`\`ucm
29+
scratch/main> auth.login
30+
\`\`\`
31+
EOF
32+
33+
while IFS= read -r line; do
34+
read -r project_source project_dest <<< "$line"
35+
# Annoyingly clone will fail if it's already been cloned, but succeed otherwise, so we just add a bad command to
36+
# force the block to always fail so we can use the :error directive.
37+
cat << EOF
38+
\`\`\`ucm:error
39+
scratch/main> clone ${project_source}
40+
scratch/main> force-failure
41+
\`\`\`
42+
43+
EOF
44+
done < <(cat "$main_project_list" "$custom_project_list") >"$pull_transcript"
45+
46+
while IFS= read -r line; do
47+
read -r project_source project_dest <<< "$line"
48+
cat << EOF
49+
\`\`\`ucm
50+
${project_source}> push ${project_dest}
51+
\`\`\`
52+
53+
EOF
54+
done < <(cat "$main_project_list" "$custom_project_list") >"$push_transcript"
55+
56+
echo "📫 Downloading projects from Share"
57+
UNISON_SHARE_HOST="https://api.unison-lang.org" ucm -C "${cache_dir}/code-cache" transcript.in-place "$pull_transcript"
58+
echo "🔑 Authenticating with local server..."
59+
UNISON_SHARE_HOST="http://localhost:5424" ucm -c "${cache_dir}/code-cache" transcript.in-place "$auth_transcript"
60+
echo "📦 Pushing projects to local server..."
61+
UNISON_SHARE_HOST="http://localhost:5424" ucm -c "${cache_dir}/code-cache" transcript.in-place "$push_transcript"

transcripts/fixtures/projects.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@unison/base/releases/4.8.0 @test/base/main

transcripts/fixtures/run.zsh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ source "${SHARE_PROJECT_ROOT}/transcripts/transcript_functions.sh"
66
# Set up database so helper scripts know it's a local db
77
pg_file "${SHARE_PROJECT_ROOT}/transcripts/sql/configure_local_database.sql"
88
pg_init_fixtures
9+
10+
source "${SHARE_PROJECT_ROOT}/transcripts/fixtures/load-from-share.sh"

0 commit comments

Comments
 (0)