1 /* 2 Copyright (c) 2019 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module main; 30 31 import std.stdio; 32 import std.array: split; 33 import std.file: readText, exists, mkdir, getcwd, write; 34 import std.process: execute, Config; 35 import std.json; 36 import std.path: absolutePath; 37 38 struct Git 39 { 40 string workDir; 41 42 this(string dir) 43 { 44 workDir = dir; 45 } 46 47 auto cmd(string[] commands) 48 { 49 return execute(["git"] ~ commands, null, Config.none, size_t.max, workDir); 50 } 51 52 auto cmd(string command) 53 { 54 return cmd([command]); 55 } 56 57 auto clone(string url) 58 { 59 return cmd(["clone"] ~ url); 60 } 61 62 auto checkout(string branchName) 63 { 64 return cmd(["checkout"] ~ branchName); 65 } 66 67 auto pull() 68 { 69 return cmd("pull"); 70 } 71 } 72 73 void run() 74 { 75 string s = readText("dependencies.json"); 76 JSONValue dubConfig = parseJSON(s); 77 JSONValue deps = dubConfig["git"]; 78 79 JSONValue[string] versions; 80 81 foreach(string depName, ref JSONValue _dep; deps) 82 { 83 string[] s = depName.split(":"); 84 string packageName = s[0]; 85 string subpackageName = ""; 86 if (s.length > 1) 87 subpackageName = s[1]; 88 89 JSONValue dep = deps[packageName]; 90 string repoUrl = dep.array[0].str; 91 string branchName = dep.array[1].str; 92 string dir = ".resolve/" ~ packageName; 93 94 if (!exists(".resolve")) 95 mkdir(".resolve"); 96 97 Git repo; 98 string dirAbs = absolutePath(dir); 99 100 writeln("Resolving ", depName, "@", branchName, " to ", "\"", dir, "\"..."); 101 102 string gitConfig = dir ~ "/.git/config"; 103 104 if (exists(gitConfig)) 105 { 106 repo = Git(dirAbs); 107 repo.pull(); 108 } 109 else 110 { 111 repo = Git(absolutePath(".resolve")); 112 repo.clone(repoUrl); 113 repo = Git(dirAbs); 114 repo.checkout(branchName); 115 } 116 117 if (subpackageName != "") 118 versions[depName] = JSONValue(["path": JSONValue(dir)]); 119 else 120 versions[packageName] = JSONValue(["path": JSONValue(dir)]); 121 } 122 123 JSONValue dubSelections; 124 125 if (exists("dub.selections.json")) 126 { 127 dubSelections = parseJSON(readText("dub.selections.json")); 128 if ("versions" in dubSelections) 129 { 130 foreach(name, value; versions) 131 dubSelections["versions"][name] = value; 132 } 133 else 134 { 135 dubSelections["versions"] = JSONValue(versions); 136 } 137 } 138 else 139 { 140 dubSelections = JSONValue( 141 [ 142 "fileVersion": JSONValue(1), 143 "versions": JSONValue(versions) 144 ]); 145 } 146 147 writeln("Updating dub.selections.json..."); 148 write("dub.selections.json", dubSelections.toPrettyString(JSONOptions.doNotEscapeSlashes)); 149 150 writeln("Done. Run \"dub build\"."); 151 } 152 153 void main() 154 { 155 run(); 156 }