1 /**
2 Generator for SublimeText project files
3 
4 Copyright: © 2014 Nicholas Londey
5 License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 Authors: Nicholas Londey
7 */
8 module dub.generators.sublimetext;
9 
10 import dub.compilers.compiler;
11 import dub.generators.generator;
12 import dub.internal.vibecompat.core.log;
13 import dub.internal.vibecompat.data.json;
14 import dub.internal.vibecompat.inet.path;
15 import dub.packagemanager;
16 import dub.project;
17 
18 import std.algorithm;
19 import std.array;
20 import std.file;
21 
22 
23 class SublimeTextGenerator : ProjectGenerator {
24 
25 	this(Project project)
26 	{
27 		super(project);
28 	}
29 
30 	override void generateTargets(GeneratorSettings settings, in TargetInfo[string] targets)
31 	{
32 		auto buildSettings = targets[m_project.name].buildSettings;
33 		logDebug("About to generate sublime project for %s.", m_project.rootPackage.name);
34 
35 		auto root = Json([
36 			"folders": targets.byValue.map!(f => targetFolderJson(f)).array.Json,
37 			"build_systems": buildSystems(settings.platform),
38 			"settings": [ "include_paths": buildSettings.importPaths.map!Json.array.Json ].Json,
39 		]);
40 
41 		auto jsonString = appender!string();
42 		writePrettyJsonString(jsonString, root);
43 
44 		string projectPath = m_project.name ~ ".sublime-project";
45 
46 		write(projectPath, jsonString.data);
47 
48 		logInfo("Project '%s' generated.", projectPath);
49 	}
50 }
51 
52 
53 private Json targetFolderJson(in ProjectGenerator.TargetInfo target)
54 {
55 	return [
56 		"name": target.pack.basePackage.name.Json,
57 		"path": target.pack.basePackage.path.toNativeString.Json,
58 		"follow_symlinks": true.Json,
59 		"folder_exclude_patterns": [".dub"].map!Json.array.Json,
60 	].Json;
61 }
62 
63 
64 private Json buildSystems(BuildPlatform buildPlatform, string workingDiretory = getcwd())
65 {
66 	static immutable BUILD_TYPES = [
67 		//"plain",
68 		"debug",
69 		"release",
70 		"release-debug",
71 		"release-nobounds",
72 		//"unittest",
73 		"docs",
74 		"ddox",
75 		"profile",
76 		"profile-gc",
77 		"cov",
78 		"unittest-cov",
79 		"syntax"
80 		];
81 
82 	string fileRegex;
83 
84 	if (buildPlatform.frontendVersion >= 2066 && buildPlatform.compiler == "dmd")
85 		fileRegex = r"^(.+)\(([0-9]+)\,([0-9]+)\)\: (.*)$";
86 	else
87 		fileRegex = r"^(.+)\(([0-9]+)\)\:() (.*)$";
88 
89 	auto arch = buildPlatform.architecture[0];
90 
91 	Json makeBuildSystem(string buildType)
92 	{
93 		return Json([
94 			"name": "DUB build " ~ buildType.Json,
95 			"cmd": ["dub", "build", "--build=" ~ buildType, "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
96 			"file_regex": fileRegex.Json,
97 			"working_dir": workingDiretory.Json,
98 			"variants": [
99 				[
100 					"name": "Run".Json,
101 					"cmd": ["dub", "run", "--build=" ~ buildType, "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
102 				].Json
103 			].array.Json,
104 		]);
105 	}
106 
107 	auto buildSystems = BUILD_TYPES.map!makeBuildSystem.array;
108 
109 	buildSystems ~= 	[
110 		"name": "DUB test".Json,
111 		"cmd": ["dub", "test", "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
112 		"file_regex": r"^(.+)\(([0-9]+)\)\:() (.*)$".Json,
113 		"working_dir": workingDiretory.Json,
114 	].Json;
115 
116 	return buildSystems.array.Json;
117 }
118 
119 unittest
120 {
121 	auto buildPlatform = BuildPlatform();
122 	buildPlatform.architecture ~= "x86_64";
123 
124 	auto result = buildPlatform.buildSystems.toString;
125 }