forked from madskristensen/TypeScriptCompileOnSave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddConfigFile.cs
More file actions
113 lines (92 loc) · 3.49 KB
/
AddConfigFile.cs
File metadata and controls
113 lines (92 loc) · 3.49 KB
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
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using System;
using System.ComponentModel.Design;
using System.IO;
using System.Threading.Tasks;
namespace TypeScriptCompileOnSave
{
internal sealed class AddConfigFile
{
private readonly Package _package;
private ProjectItem _item;
private AddConfigFile(Package package, OleMenuCommandService commandService)
{
_package = package;
var cmdId = new CommandID(PackageGuids.guidCompilePackageCmdSet, PackageIds.AddConfigFileId);
var cmd = new OleMenuCommand(Execute, cmdId);
cmd.BeforeQueryStatus += BeforeQueryStatus;
commandService.AddCommand(cmd);
}
public static AddConfigFile Instance
{
get;
private set;
}
private IServiceProvider ServiceProvider
{
get { return _package; }
}
public static void Initialize(Package package, OleMenuCommandService commandService)
{
Instance = new AddConfigFile(package, commandService);
}
private void BeforeQueryStatus(object sender, EventArgs e)
{
var button = (OleMenuCommand)sender;
button.Visible = button.Enabled = false;
button.Text = "Transpile to JavaScript";
DTE2 dte = VsHelpers.GetService<DTE, DTE2>();
_item = dte.SelectedItems.Item(1).ProjectItem;
if (dte.SelectedItems.MultiSelect || !Transpiler.IsProjectSupported(_item.ContainingProject))
return;
string fileName = _item.FileNames[1];
if (!Transpiler.IsFileSupported(fileName))
return;
button.Visible = true;
if (Transpiler.IsBuildingOrDebugging(dte))
return;
if (VsHelpers.FileExistAtOrAbove(fileName, Constants.ConfigFileName, out string cwd))
{
button.Text = $"Transpile to JavaScript ({Constants.ConfigFileName} found)";
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
private async void Execute(object sender, EventArgs e)
{
if (_item == null || _item.ContainingProject == null)
return;
try
{
string projectRoot = _item.ContainingProject.Properties.Item("FullPath").Value.ToString();
if (Directory.Exists(projectRoot))
{
string configPath = await CreateConfigFile(projectRoot);
VsHelpers.OpenFileAndSelect(_item.DTE, configPath);
TranspilerStatus status = await _item.Transpile();
}
}
catch (Exception ex)
{
Logger.Log(ex);
}
}
private async Task<string> CreateConfigFile(string projectRoot)
{
string file = _item.FileNames[1].Substring(projectRoot.Length + 1).Replace("\\", "/");
string configPath = Path.Combine(projectRoot, Constants.ConfigFileName);
string content = string.Format(Constants.DefaultTsConfig, file);
using (var fs = new FileStream(configPath, FileMode.Create))
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
await fs.WriteAsync(buffer, 0, buffer.Length);
}
return configPath;
}
}
}