Skip to content

Instantly share code, notes, and snippets.

@damosuzuki
Created September 24, 2017 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damosuzuki/8454d5668a7bf5ac8fa6044c1bee9b5e to your computer and use it in GitHub Desktop.
Save damosuzuki/8454d5668a7bf5ac8fa6044c1bee9b5e to your computer and use it in GitHub Desktop.
clone-unity-projects
"use strict";
// Usage
// $ npm install nodegit
// $ node clone-projects.js
const git = require("nodegit");
const url = require("url");
const os = require("os");
const path = require("path");
const { spawn } = require("child_process");
const UNITY_MAC = "/Applications/Unity/Unity.app/Contents/MacOS/Unity";
const UNITY_WIN = "C:\\Program Files\\Unity\\Editor\\Unity.exe";
let projects = [
{ url: "https://github.com/TimeWalkOrg/timewalk.git", branch: "origin/night-day-cycle" },
{ url: "https://github.com/TimeWalkOrg/location-mill-valley-ca.git", branch: "origin/initial-layout" }
];
let clonedProjects = [];
let processing = false;
function cloneProjects(){
projects.forEach(proj => {
let path = url.parse(proj.url).pathname;
let repoPath = "SCRATCH-" + path.split("/").pop();
console.log(`Cloning ${proj.url}#${proj.branch} to ${repoPath}`);
git.Clone(proj.url, repoPath)
.then((repo) => {
return repo.checkoutBranch(proj.branch);
})
.then(() => {
proj.path = repoPath;
clonedProjects.push(proj);
processProjects();
})
.catch(function(err) { console.log(err); });
});
}
function processProjects(){
if(processing) return;
if(clonedProjects.length === 0) {
console.log("Processing complete.");
return;
}
const project = clonedProjects.shift();
const projectPath = project.path;
const commands = [
"-quit",
"-batchmode",
"-projectPath", path.join(process.cwd(), projectPath),
"-executeMethod", "ExternalPackageManager.ImportAll"
];
console.log("Processing " + projectPath);
processing = true;
const unityImport = spawn(os.platform().indexOf("win") === 0 ? UNITY_WIN : UNITY_MAC, commands);
unityImport.stdout.on("data", (data) => {
console.log(`Unity Import: ${data}`);
});
unityImport.stderr.on("data", (data) => {
console.error(`Unity Import (error): ${data}`);
});
unityImport.on("close", (code) => {
console.log(`Unity Import exited with code ${code}`);
processing = false;
processProjects();
});
}
if(require.main === module) {
cloneProjects(projects.map( project => project.url ));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment