-
Notifications
You must be signed in to change notification settings - Fork 1
/
CactusTemplateManager.sc
159 lines (134 loc) · 5.12 KB
/
CactusTemplateManager.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
CactusTemplateManager { var <cactus, <templatesDir;
*new { arg cactus;
^super.newCopyArgs(cactus).init;
}
init {
templatesDir = CactusTemplateManager.filenameSymbol.asString.dirname;
templatesDir = templatesDir ++ "/templates/"
}
runTemplate { arg templateName, options = (); var path;
path = PathName.new(templatesDir ++ templateName);
this.copyTemplateBaseFiles(path, options);
this.copyTemplateSubFolderFiles(path, options);
}
copyTemplateBaseFiles { arg path, options;
path.files.do{ arg file;
if(file.fileName != "info.yaml"){
this.createFromTemplateFile(
sourcePath: file,
targetDir: options.targetDir,
options: options
);
};
};
}
copyTemplateSubFolderFiles { arg path, options;
path.folders.do{ arg folder;
folder.files.do{ arg file; var target;
target = options.targetDir +/+ file.folderName;
File.mkdir(target.standardizePath);
this.createFromTemplateFile(
sourcePath: file,
targetDir: target,
options: options
);
};
};
}
createFromTemplateFile {
arg sourcePath, targetDir, options;
var targetFilePath, generatedFileContent;
targetFilePath = targetDir +/+ sourcePath.fileName;
targetFilePath = targetFilePath.standardizePath;
generatedFileContent = this.parseTemplateFile(
path: sourcePath.fullPath,
options: options);
this.carefullyCreateFile(targetFilePath, generatedFileContent)
}
carefullyCreateFile{ arg targetFilePath, generatedFileContent;
if (File.exists(targetFilePath).not
or:{File.readAllString(targetFilePath).size == 0}, {
this.createTheFile(targetFilePath, generatedFileContent)
},{
this.createBackupFileCopy(targetFilePath, generatedFileContent);
this.createTheFile(targetFilePath, generatedFileContent);
});
}
createTheFile { arg targetFilePath, generatedFileContent;
File.new(targetFilePath.standardizePath, "w").write(generatedFileContent).close;
("File" + targetFilePath.basename + "has been created.").postln;
}
createBackupFileCopy { arg targetFilePath;
File.copy(
targetFilePath.standardizePath,
targetFilePath.standardizePath.splitext[0] ++ "-" ++ Date.getDate.stamp ++ ".scd");
("-> A file named" + targetFilePath.basename + "was already there!").postln;
"-> Renamed old file".postln;
}
parseTemplateFile { arg path, options;
File.use(path.standardizePath, "r", {
arg file; var string;
string = file.readAllString;
options.pairsDo{ arg key, value;
string = string.replace("{{" ++ key.asString ++ "}}", value.value.asString);
};
^string;
});
}
// Drafts
gui {
this.browseFromPath(templatesDir;);
}
browseFromPath { arg path;
var window, listView, textView;
var windowRect, applyButton, cancelButton;
var winWidth, winHeight, rawPath = path;
winWidth = 815;
winHeight = 453;
path = PathName(path);
windowRect = Rect(
GUI.window.screenBounds.width-winWidth*0.5,
GUI.window.screenBounds.height-winHeight*0.5,
winWidth, winHeight);
window = Window.new( "Template Browser", windowRect, resizable: false).front;
window.view.decorator = FlowLayout( window.view.bounds );
window.background_(Color.fromHexString("#282828"));
listView = EZListView.new(window,200@400);
listView.font = Font("Monaco", 14);
textView = TextView(window, 600@400).background_(Color.white);
textView.editable = false;
StaticText(window, Rect(width: 540 , height: 40));
cancelButton = Button(window, Rect(width: 128, height: 40) );
cancelButton.states = [["Cancel", Color.white, Color.grey]];
cancelButton.canFocus = false;
applyButton = Button(window, Rect(width: 128, height: 40) );
applyButton.states = [["Apply", Color.white, Color.grey]];
applyButton.canFocus = false;
path.folders.do{ arg item; var name;
name = item.folderName;
listView.addItem(
this.getInfo(name, \name, path: path.fullPath),
{
var title, body, example, credits, tags;
title = "📜 " + this.getInfo(name, \name, path: path.fullPath) + "\n";
body = "\n" + this.getInfo(name, \description, path: path.fullPath).stripWhiteSpace + "\n\n";
textView.string = title ++ body;
// From Title to body
textView.setFont(Font("Palatino", 48), 0, title.size - 4);
textView.setStringColor(Color.fromHexString("#9d817f"), 0, title.size - 4);
// From body to end
textView.setFont(Font("Palatino", 20, italic: true), title.size - 4, 10000);
textView.setStringColor(Color.black, title.size - 4, 10000);
cancelButton.action = {window.close};
applyButton.action = {cactus.runTemplate(name.asSymbol); window.close;};
}
);
};
if(listView.items.size > 0, { listView.valueAction = 0 });
}
getInfo { arg name, key, path = templatesDir; var yamlDictionary;
path = path +/+ name +/+ "info.yaml";
yamlDictionary = path.standardizePath.parseYAMLFile;
^yamlDictionary.at(key.asString);
}
}