|
| 1 | +import * as cp from "child_process"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as os from "os"; |
| 5 | +import { Uri } from "vscode"; |
| 6 | +import { SpawnOptions, ChildProcess } from "child_process"; |
| 7 | + |
| 8 | +const tempDir = os.tmpdir(); |
| 9 | +var tempDirList: string[] = []; |
| 10 | + |
| 11 | +export function spawn( |
| 12 | + command: string, |
| 13 | + args?: string[], |
| 14 | + options?: SpawnOptions |
| 15 | +): ChildProcess { |
| 16 | + let proc = cp.spawn(command, args, options); |
| 17 | + |
| 18 | + // let fullCommand = "command: " + command; |
| 19 | + |
| 20 | + // if (args) { |
| 21 | + // fullCommand += ' "' + args.join('" "') + '"'; |
| 22 | + // } |
| 23 | + // console.log(fullCommand); |
| 24 | + |
| 25 | + // proc.stdout.on("data", function(data) { |
| 26 | + // console.log("stdout: " + data.toString()); |
| 27 | + // }); |
| 28 | + |
| 29 | + // proc.stderr.on("data", function(data) { |
| 30 | + // console.log("stderr: " + data.toString()); |
| 31 | + // }); |
| 32 | + |
| 33 | + // proc.on("exit", function(code) { |
| 34 | + // console.log("child process exited with code " + code.toString()); |
| 35 | + // }); |
| 36 | + |
| 37 | + return proc; |
| 38 | +} |
| 39 | + |
| 40 | +export function newTempDir(prefix: string) { |
| 41 | + const fullpath = fs.mkdtempSync(path.join(tempDir, prefix)); |
| 42 | + |
| 43 | + tempDirList.push(fullpath); |
| 44 | + |
| 45 | + return fullpath; |
| 46 | +} |
| 47 | + |
| 48 | +export function createRepoServer() { |
| 49 | + return new Promise<string>((resolve, reject) => { |
| 50 | + const fullpath = newTempDir("svn_server_"); |
| 51 | + const dirname = path.basename(fullpath); |
| 52 | + |
| 53 | + if (fs.existsSync(fullpath)) { |
| 54 | + destroyPath(fullpath); |
| 55 | + } |
| 56 | + |
| 57 | + let proc = spawn("svnadmin", ["create", dirname], { cwd: tempDir }); |
| 58 | + |
| 59 | + proc.once("exit", exitCode => { |
| 60 | + if (exitCode === 0) { |
| 61 | + const url = "file:///" + fullpath.replace(/\\/g, "/"); |
| 62 | + resolve(url); |
| 63 | + } |
| 64 | + reject(); |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +export function importToRepoServer( |
| 70 | + url: string, |
| 71 | + path: string, |
| 72 | + message = "imported", |
| 73 | + cwd?: string |
| 74 | +) { |
| 75 | + return new Promise<void>((resolve, reject) => { |
| 76 | + let proc = spawn("svn", ["import", path, url, "-m", message], { |
| 77 | + cwd: cwd |
| 78 | + }); |
| 79 | + |
| 80 | + proc.once("exit", exitCode => { |
| 81 | + if (exitCode === 0) { |
| 82 | + resolve(); |
| 83 | + } |
| 84 | + reject(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +export async function createStandardLayout( |
| 90 | + url: string, |
| 91 | + trunk = "trunk", |
| 92 | + branches = "branches", |
| 93 | + tags = "tags" |
| 94 | +) { |
| 95 | + const fullpath = newTempDir("svn_layout_"); |
| 96 | + const dirname = path.basename(fullpath); |
| 97 | + |
| 98 | + fs.mkdirSync(path.join(fullpath, trunk)); |
| 99 | + fs.mkdirSync(path.join(fullpath, branches)); |
| 100 | + fs.mkdirSync(path.join(fullpath, tags)); |
| 101 | + |
| 102 | + await importToRepoServer(url, fullpath, "Created Standard Layout"); |
| 103 | + |
| 104 | + destroyPath(fullpath); |
| 105 | +} |
| 106 | + |
| 107 | +export function createRepoCheckout(url: string) { |
| 108 | + return new Promise<string>((resolve, reject) => { |
| 109 | + const fullpath = newTempDir("svn_checkout_"); |
| 110 | + |
| 111 | + let proc = spawn("svn", ["checkout", url, fullpath], { cwd: tempDir }); |
| 112 | + |
| 113 | + proc.once("exit", exitCode => { |
| 114 | + if (exitCode === 0) { |
| 115 | + resolve(fullpath); |
| 116 | + } |
| 117 | + reject(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +export function destroyPath(fullPath: string) { |
| 123 | + fullPath = fullPath.replace(/^file\:\/\/\//, ""); |
| 124 | + |
| 125 | + if (!fs.existsSync(fullPath)) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if (!fs.lstatSync(fullPath).isDirectory()) { |
| 130 | + fs.unlinkSync(fullPath); |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + const files = fs.readdirSync(fullPath); |
| 135 | + for (let file of files) { |
| 136 | + destroyPath(path.join(fullPath, file)); |
| 137 | + } |
| 138 | + fs.rmdirSync(fullPath); |
| 139 | + return true; |
| 140 | +} |
| 141 | + |
| 142 | +export function destroyAllTempPaths() { |
| 143 | + for (let path of tempDirList) { |
| 144 | + destroyPath(path); |
| 145 | + } |
| 146 | +} |
0 commit comments