forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
1package models 2 3import ( 4 "fmt" 5 "path" 6 "strings" 7 8 "tangled.sh/tangled.sh/core/api/tangled" 9) 10 11// checkoutStep checks out the specified ref in the cloned repository. 12func checkoutStep(twf tangled.Pipeline_Workflow, tr tangled.Pipeline_TriggerMetadata) Step { 13 if twf.Clone.Skip { 14 return Step{} 15 } 16 17 var ref string 18 switch tr.Kind { 19 case "push": 20 ref = tr.Push.Ref 21 case "pull_request": 22 ref = tr.PullRequest.TargetBranch 23 24 // TODO: this needs to be specified in lexicon 25 case "manual": 26 ref = tr.Repo.DefaultBranch 27 } 28 29 checkoutCmd := fmt.Sprintf("git config advice.detachedHead false; git checkout --progress --force %s", ref) 30 31 return Step{ 32 Command: checkoutCmd, 33 Name: "Checkout ref " + ref, 34 } 35} 36 37// cloneOptsAsSteps processes clone options and adds corresponding steps 38// to the beginning of the workflow's step list if cloning is not skipped. 39func cloneStep(twf tangled.Pipeline_Workflow, tr tangled.Pipeline_TriggerRepo, dev bool) Step { 40 if twf.Clone.Skip { 41 return Step{} 42 } 43 44 uri := "https://" 45 if dev { 46 uri = "http://" 47 tr.Knot = strings.ReplaceAll(tr.Knot, "localhost", "host.docker.internal") 48 } 49 50 cloneUrl := uri + path.Join(tr.Knot, tr.Did, tr.Repo) 51 cloneCmd := []string{"git", "clone", cloneUrl, "."} 52 53 // default clone depth is 1 54 cloneDepth := 1 55 if twf.Clone.Depth > 1 { 56 cloneDepth = int(twf.Clone.Depth) 57 cloneCmd = append(cloneCmd, []string{"--depth", fmt.Sprintf("%d", cloneDepth)}...) 58 } 59 60 if twf.Clone.Submodules { 61 cloneCmd = append(cloneCmd, "--recursive") 62 } 63 64 fmt.Println(strings.Join(cloneCmd, " ")) 65 66 cloneStep := Step{ 67 Command: strings.Join(cloneCmd, " "), 68 Name: "Clone repository into workspace", 69 } 70 return cloneStep 71} 72 73// dependencyStep processes dependencies defined in the workflow. 74// For dependencies using a custom registry (i.e. not nixpkgs), it collects 75// all packages and adds a single 'nix profile install' step to the 76// beginning of the workflow's step list. 77func dependencyStep(twf tangled.Pipeline_Workflow) Step { 78 var customPackages []string 79 80 for _, d := range twf.Dependencies { 81 registry := d.Registry 82 packages := d.Packages 83 84 if registry == "nixpkgs" { 85 continue 86 } 87 88 // collect packages from custom registries 89 for _, pkg := range packages { 90 customPackages = append(customPackages, fmt.Sprintf("'%s#%s'", registry, pkg)) 91 } 92 } 93 94 if len(customPackages) > 0 { 95 installCmd := "nix --extra-experimental-features nix-command --extra-experimental-features flakes profile install" 96 cmd := fmt.Sprintf("%s %s", installCmd, strings.Join(customPackages, " ")) 97 installStep := Step{ 98 Command: cmd, 99 Name: "Install custom dependencies", 100 Environment: map[string]string{ 101 "NIX_NO_COLOR": "1", 102 "NIX_SHOW_DOWNLOAD_PROGRESS": "0", 103 }, 104 } 105 return installStep 106 } 107 return Step{} 108}