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.data.json;
13 import dub.internal.vibecompat.inet.path;
14 import dub.internal.logging;
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, settings.toolWorkingDirectory.toNativeString()),
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("Generated", Color.green, "%s", 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)
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 		"cov-ctfe",
79 		"unittest-cov",
80 		"unittest-cov-ctfe",
81 		"syntax"
82 		];
83 
84 	string fileRegex;
85 
86 	if (buildPlatform.frontendVersion >= 2066 && buildPlatform.compiler == "dmd")
87 		fileRegex = r"^(.+)\(([0-9]+)\,([0-9]+)\)\: (.*)$";
88 	else
89 		fileRegex = r"^(.+)\(([0-9]+)\)\:() (.*)$";
90 
91 	auto arch = buildPlatform.architecture[0];
92 
93 	Json makeBuildSystem(string buildType)
94 	{
95 		return Json([
96 			"name": "DUB build " ~ buildType.Json,
97 			"cmd": ["dub", "build", "--build=" ~ buildType, "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
98 			"file_regex": fileRegex.Json,
99 			"working_dir": workingDiretory.Json,
100 			"variants": [
101 				[
102 					"name": "Run".Json,
103 					"cmd": ["dub", "run", "--build=" ~ buildType, "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
104 				].Json
105 			].array.Json,
106 		]);
107 	}
108 
109 	auto buildSystems = BUILD_TYPES.map!makeBuildSystem.array;
110 
111 	buildSystems ~= 	[
112 		"name": "DUB test".Json,
113 		"cmd": ["dub", "test", "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
114 		"file_regex": r"^(.+)\(([0-9]+)\)\:() (.*)$".Json,
115 		"working_dir": workingDiretory.Json,
116 	].Json;
117 
118 	return buildSystems.array.Json;
119 }
120 
121 unittest
122 {
123 	auto buildPlatform = BuildPlatform();
124 	buildPlatform.architecture ~= "x86_64";
125 
126 	auto result = buildPlatform.buildSystems(getcwd()).toString;
127 }