1 /*
2 Copyright (c) 2014 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 exdep;
30 
31 import std.stdio;
32 import std.digest.crc;
33 import std.format;
34 import std.array;
35 import std..string;
36 import std.file;
37 import std.path;
38 import std.process;
39 
40 import cmdopt;
41 
42 string home(string path, CmdOptions opt)
43 {
44     string homeDir;
45     version(Posix)
46         homeDir = environment.get("HOME");
47     version(Windows)
48         homeDir = environment.get("APPDATA").replace("\\", "/");
49     if (opt.local)
50         homeDir = ".";
51     return homeDir ~ "/" ~ path;
52 }
53 
54 class Exdep
55 {
56     string descriptor;
57     string id;
58 
59     string vcs;
60     string hostname;
61     string path;
62     
63     string subdir = "";
64     string location;
65     
66     string srcDir;
67     
68     bool useHTTPS = false;
69 
70     this(string desc, bool https, CmdOptions cmd)
71     {
72         descriptor = desc;
73         useHTTPS = https;
74         id = crc32Of(descriptor).crcHexString;
75         string name;
76         try
77         {
78             formattedRead(descriptor, "%s:%s:%s", &name, &path, &subdir);
79         }
80         catch(Exception) {}
81         finally {}
82         
83         // TODO: check the result
84 
85         auto pos = indexOf(name, "@");
86         if (pos >= 0)
87             formattedRead(name, "%s@%s", &vcs, &hostname);
88         else
89             vcs = name;
90 
91         if (vcs != "local")
92             // TODO: use config to override this
93             location = home(format(".cook/remote/%s", id), cmd);
94         else
95             location = path;
96             
97         if (subdir.length)
98             srcDir = location ~ "/" ~ subdir;
99         else
100             srcDir = location;
101     }
102 
103     void print()
104     {
105         writefln("id: %s", id);
106         writefln("VCS: %s", vcs);
107         writefln("Host: %s", hostname);
108         writefln("Path: %s", path);
109     }
110 
111     void fetch(CmdOptions ops)
112     {
113         if (vcs != "local")
114         {
115             if (exists() && isDir(location))
116             {
117                 if (ops.rebuild)
118                 {
119                     writefln("Fetching %s (%s from %s)...", id, path, hostname);
120                     pull();
121                 }
122             }
123             else
124             {
125                 writefln("Fetching %s (%s from %s)...", id, path, hostname);
126                 clone();
127             }
128         }
129     }
130 
131     bool exists()
132     {
133         return (std.file.exists(location));
134     }
135 
136     void clone()
137     {
138         if (vcs == "git")
139         {
140             string command;
141             if (useHTTPS)
142                 command = format("git clone https://%s/%s.git %s", hostname, path, location);
143             else
144                 command = format("git clone git@%s:%s.git %s", hostname, path, location);
145             writeln(command);
146             executeShell(command);
147         }
148     }
149 
150     void pull()
151     {
152         if (vcs == "git")
153         {
154             string command = format("cd %s && git pull", location);
155             writeln(command);
156             executeShell(command);
157         }
158     }
159 }
160