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.compiler;
21 import std.file;
22 import std.path;
23 import std.range;
24 import std..string;
25 
26 
27 class SublimeTextGenerator : ProjectGenerator {
28 
29 	this(Project project)
30 	{
31 		super(project);
32 	}
33 
34 	override void generateTargets(GeneratorSettings settings, in TargetInfo[string] targets)
35 	{
36 		auto buildSettings = targets[m_project.name].buildSettings;
37 		logDebug("About to generate sublime project for %s.", m_project.rootPackage.name);
38 
39 		auto root = Json([
40 			"folders": targets.byValue.map!(f => targetFolderJson(f)).array.Json,
41 			"build_systems": buildSystems(settings.platform),
42 			"settings": [ "include_paths": buildSettings.importPaths.map!Json.array.Json ].Json,
43 		]);
44 
45 		auto jsonString = appender!string();
46 		writePrettyJsonString(jsonString, root);
47 
48 		string projectPath = m_project.name ~ ".sublime-project";
49 
50 		write(projectPath, jsonString.data);
51 
52 		logInfo("Project '%s' generated.", projectPath);
53 	}
54 }
55 
56 
57 private Json targetFolderJson(in ProjectGenerator.TargetInfo target)
58 {
59 	return [
60 		"name": target.pack.basePackage.name.Json,
61 		"path": target.pack.basePackage.path.toNativeString.Json,
62 		"follow_symlinks": true.Json,
63 		"folder_exclude_patterns": [".dub"].map!Json.array.Json,
64 	].Json;
65 }
66 
67 
68 private Json buildSystems(BuildPlatform buildPlatform, string workingDiretory = getcwd())
69 {
70 	enum BUILD_TYPES = [
71 		//"plain",
72 		"debug",
73 		"release",
74 		"release-debug",
75 		"release-nobounds",
76 		//"unittest",
77 		"docs",
78 		"ddox",
79 		"profile",
80 		"profile-gc",
81 		"cov",
82 		"unittest-cov",
83 		];
84 
85 	string fileRegex;
86 
87 	if (buildPlatform.frontendVersion >= 2066 && buildPlatform.compiler == "dmd")
88 		fileRegex = r"^(.+)\(([0-9]+)\,([0-9]+)\)\: (.*)$";
89 	else
90 		fileRegex = r"^(.+)\(([0-9]+)\)\:() (.*)$";
91 
92 	auto arch = buildPlatform.architecture[0];
93 
94 	Json makeBuildSystem(string buildType)
95 	{
96 		return Json([
97 			"name": "DUB build " ~ buildType.Json,
98 			"cmd": ["dub", "build", "--build=" ~ buildType, "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
99 			"file_regex": fileRegex.Json,
100 			"working_dir": workingDiretory.Json,
101 			"variants": [
102 				[
103 					"name": "Run".Json,
104 					"cmd": ["dub", "run", "--build=" ~ buildType, "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
105 				].Json
106 			].array.Json,
107 		]);
108 	}
109 
110 	auto buildSystems = BUILD_TYPES.map!makeBuildSystem.array;
111 
112 	buildSystems ~= 	[
113 		"name": "DUB test".Json,
114 		"cmd": ["dub", "test", "--arch=" ~ arch, "--compiler="~buildPlatform.compilerBinary].map!Json.array.Json,
115 		"file_regex": r"^(.+)\(([0-9]+)\)\:() (.*)$".Json,
116 		"working_dir": workingDiretory.Json,
117 	].Json;
118 
119 	return buildSystems.array.Json;
120 }
121 
122 unittest
123 {
124 	auto buildPlatform = BuildPlatform();
125 	buildPlatform.architecture ~= "x86_64";
126 
127 	auto result = buildPlatform.buildSystems.toString;
128 }