|
| 1 | +## `wing project clone` and `wing project adopt` — getting a project into the registry. |
| 2 | +## |
| 3 | +## Cloning already tells you everything the registry wants to know, and doing it by hand is three |
| 4 | +## commands that each repeat part of the last one: clone somewhere, remember where, register it. |
| 5 | +## This is those three, once, and it works the same whether the machine you want it on is this one. |
| 6 | + |
| 7 | +import std/[os, osproc, strutils] |
| 8 | + |
| 9 | +import ../cliargs |
| 10 | +import ../discovery |
| 11 | +import ../projects/layout |
| 12 | +import ../projects/locate |
| 13 | +import ../remote |
| 14 | +import ../store/machines |
| 15 | +import ../store/projects |
| 16 | +import ../types |
| 17 | +import ../util |
| 18 | + |
| 19 | +proc registerProject(name, path, machine, language: string): bool = |
| 20 | + ## True when it was added rather than already known. The same path on the same machine is the same |
| 21 | + ## project, which is what makes running this twice harmless. |
| 22 | + let file = ensureProjectsFile() |
| 23 | + var projects = parseProjects(file) |
| 24 | + for project in projects: |
| 25 | + if project.path == path and project.machine == machine: |
| 26 | + return false |
| 27 | + let stamp = nowStamp() |
| 28 | + projects.add(Project(name: name, path: path, machine: machine, |
| 29 | + namespace: "default", language: language, tags: @[], createdAt: stamp, |
| 30 | + updatedAt: stamp)) |
| 31 | + writeProjects(file, projects) |
| 32 | + true |
| 33 | + |
| 34 | +proc detectLanguage(path: string): string = |
| 35 | + let detected = detectProject(path) |
| 36 | + if detected.found: detected.project.language else: "" |
| 37 | + |
| 38 | +proc handleClone*(argsIn: seq[string]) = |
| 39 | + var args = argsIn |
| 40 | + let onMachine = popValue(args, ["-m", "--machine"]) |
| 41 | + let intoRoot = popValue(args, ["--root"], codeRoot()) |
| 42 | + let atPath = popValue(args, ["--path"]) |
| 43 | + let branch = popValue(args, ["-b", "--branch"]) |
| 44 | + let dryRun = popFlag(args, ["--dry-run", "-n"]) |
| 45 | + rejectUnknownOptions(args) |
| 46 | + requireArgs(args, 1, |
| 47 | + "wing project clone URL|owner/name [--machine NAME] [--root DIR] [--branch B]") |
| 48 | + |
| 49 | + let repo = parseRepoRef(args[0]) |
| 50 | + if repo.name.len == 0: |
| 51 | + die("Could not read a repository out of '" & args[0] & "'", 2) |
| 52 | + let target = if atPath.len > 0: atPath else: layoutPath(intoRoot, repo) |
| 53 | + |
| 54 | + var command = "git clone" |
| 55 | + if branch.len > 0: |
| 56 | + command.add(" --branch " & quoteShell(branch)) |
| 57 | + command.add(" " & quoteShell(repo.url) & " " & quoteShell(target)) |
| 58 | + |
| 59 | + if dryRun: |
| 60 | + echo (if onMachine.len > 0: onMachine & ": " else: "") & command |
| 61 | + return |
| 62 | + |
| 63 | + if onMachine.len > 0: |
| 64 | + let machines = parseMachines(ensureMachinesFile()) |
| 65 | + var found = false |
| 66 | + var machine: Machine |
| 67 | + for candidate in machines: |
| 68 | + if candidate.name == onMachine: |
| 69 | + machine = candidate |
| 70 | + found = true |
| 71 | + break |
| 72 | + if not found: |
| 73 | + die("Machine '" & onMachine & "' not found", 2) |
| 74 | + # mkdir first: git will not create the parents, and the layout is several levels deep. |
| 75 | + let script = "mkdir -p " & quoteShell(parentDir(target)) & " && " & command |
| 76 | + let results = runOn(@[RemoteTarget(machine: machine, host: firstHost( |
| 77 | + machine))], script, 0) |
| 78 | + for r in results: |
| 79 | + for line in r.output.strip().splitLines(): |
| 80 | + if line.strip().len > 0: |
| 81 | + echo " " & line |
| 82 | + if r.exitCode != 0: |
| 83 | + die("clone failed on " & onMachine, 1) |
| 84 | + # The language is read on the machine that has the files, since this one does not. |
| 85 | + let asked = runOn(@[RemoteTarget(machine: machine, host: firstHost( |
| 86 | + machine))], "cd " & quoteShell(target) & " && ls", 0) |
| 87 | + var language = "" |
| 88 | + if asked.len > 0 and asked[0].exitCode == 0: |
| 89 | + for line in asked[0].output.splitLines(): |
| 90 | + case line.strip() |
| 91 | + of "Cargo.toml": language = "rust" |
| 92 | + of "go.mod": language = "go" |
| 93 | + of "pyproject.toml": language = "python" |
| 94 | + of "package.json": language = "node" |
| 95 | + of "build.zig": language = "zig" |
| 96 | + of "dub.json": language = "d" |
| 97 | + else: |
| 98 | + if line.strip().endsWith(".nimble"): language = "nim" |
| 99 | + discard registerProject(repo.name, target, onMachine, language) |
| 100 | + echo repo.name & " -> " & onMachine & ":" & target |
| 101 | + return |
| 102 | + |
| 103 | + createDir(parentDir(target)) |
| 104 | + if execCmd(command) != 0: |
| 105 | + die("clone failed", 1) |
| 106 | + discard registerProject(repo.name, target, "", detectLanguage(target)) |
| 107 | + echo repo.name & " -> " & target |
| 108 | + |
| 109 | +proc handleAdopt*(argsIn: seq[string]) = |
| 110 | + ## An existing checkout, moved into the layout and registered. `--in-place` keeps it where it is |
| 111 | + ## and only registers, which is what you want for a directory that other things point at. |
| 112 | + var args = argsIn |
| 113 | + let inPlace = popFlag(args, ["--in-place", "--here"]) |
| 114 | + let intoRoot = popValue(args, ["--root"], codeRoot()) |
| 115 | + let dryRun = popFlag(args, ["--dry-run", "-n"]) |
| 116 | + rejectUnknownOptions(args) |
| 117 | + let source = expandFilename(if args.len > 0: args[0] else: getCurrentDir()) |
| 118 | + if not dirExists(source): |
| 119 | + die("'" & source & "' is not a directory", 2) |
| 120 | + |
| 121 | + if inPlace: |
| 122 | + let name = source.lastPathPart |
| 123 | + if dryRun: |
| 124 | + echo "register " & name & " -> " & source |
| 125 | + return |
| 126 | + if registerProject(name, source, "", detectLanguage(source)): |
| 127 | + echo "registered " & name & " -> " & source |
| 128 | + else: |
| 129 | + echo name & " was already registered" |
| 130 | + return |
| 131 | + |
| 132 | + # Where it belongs is read from the remote it was cloned from, which is the only thing that knows. |
| 133 | + # The exit code decides, not the output: execCmdEx merges stderr, so a directory that is not a |
| 134 | + # repository answers with git's error text -- which has slashes and a colon in it and parses as a |
| 135 | + # URL if you only check that something came back. |
| 136 | + let asked = execCmdEx("git -C " & quoteShell(source) & " remote get-url origin") |
| 137 | + let remote = if asked.exitCode == 0: asked.output.strip() else: "" |
| 138 | + if remote.len == 0: |
| 139 | + die("'" & source & "' has no origin remote, so there is no layout to move it into — " & |
| 140 | + "--in-place registers it where it is", 2) |
| 141 | + let repo = parseRepoRef(remote) |
| 142 | + if repo.name.len == 0: |
| 143 | + die("Could not read a repository out of '" & remote & "'", 2) |
| 144 | + let target = layoutPath(intoRoot, repo) |
| 145 | + if target == source: |
| 146 | + if registerProject(repo.name, source, "", detectLanguage(source)): |
| 147 | + echo "registered " & repo.name & " -> " & source |
| 148 | + else: |
| 149 | + echo repo.name & " is already where it belongs" |
| 150 | + return |
| 151 | + if dirExists(target): |
| 152 | + die("'" & target & "' already exists", 2) |
| 153 | + |
| 154 | + if dryRun: |
| 155 | + echo source & " -> " & target |
| 156 | + return |
| 157 | + createDir(parentDir(target)) |
| 158 | + moveDir(source, target) |
| 159 | + discard registerProject(repo.name, target, "", detectLanguage(target)) |
| 160 | + echo repo.name & ": " & source & " -> " & target |
0 commit comments