-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
167 lines (135 loc) · 3.31 KB
/
extension.js
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
160
161
162
163
164
165
166
167
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
/**
* @param {vscode.ExtensionContext} context
*/
function openFile(fileName) {
vscode.workspace
.openTextDocument(fileName)
.then(vscode.window.showTextDocument);
}
function prompt(fileName, cb) {
let options = {
placeHolder: `Create ${fileName}?`
}
vscode.window.showQuickPick(["Yes", "No"], options)
.then(function(answer) {
if (answer === "Yes") {
cb();
}
});
}
function getRelated(file) {
if (isSpec(file)) {
return specToCode(file);
} else {
return codeToSpec(file);
}
}
function isSpec(file) {
return file.indexOf('_test.rb') > -1;
}
function codeToSpec(file) {
let viewRegex = /erb$|haml$|slim$/
let isViewFile = file.match(viewRegex);
if (isViewFile) {
return file
.replace('/app/', '/test/')
}
file = file.replace('.rb', '_test.rb');
let isLibFile = file.indexOf('/lib/') > -1;
if (isLibFile) {
return file.replace('/lib/', '/test/lib/');
}
return file.replace('/app/', '/test/');
}
function specToCode(file) {
let viewRegex = /(.erb|.haml|.slim)_test.rb$/;
let isViewFile = file.match(viewRegex);
if (isViewFile) {
return file
.replace('_test.rb', '')
.replace('/test', '/app');
}
file = file.replace('_test.rb', '.rb');
let isLibFile = file.indexOf('/test/lib/') > -1;
if (isLibFile) {
return file.replace('/test/lib/', '/lib/');
}
return file.replace('/test/', '/app/');
}
function snake2Pascal(str) {
str +='';
str = str.split('_');
function upper( str ){
return str.slice(0,1).toUpperCase() + str.slice(1,str.length);
}
for(var i=0;i<str.length;i++){
var str2 = str[i].split('/');
for(var j=0;j<str2.length;j++){
str2[j] = upper(str2[j]);
}
str[i] = str2.join('');
}
return str.join('');
}
function testContent(file) {
const split = file.split('/')
const appIndex = split.indexOf('app')
const startIndex = appIndex + 2
const testTitle = split.slice(startIndex, split.length).map(x => snake2Pascal(x)).join('::').replace('.rb', 'Test')
let testClass = 'ActiveSupport::TestCase'
switch(split[appIndex + 1]) {
case 'components':
testClass = 'ViewComponent::TestCase'
break
case 'helpers':
testClass = 'ActionView::TestCase'
break
case 'controllers':
testClass = 'ActionDispatch::IntegrationTest'
break
default:
break
}
const text = `require 'test_helper'
class ${testTitle} < ${testClass}
test "" do
end
end
`;
return text;
}
function activate(context) {
let disposable = vscode.commands.registerCommand('rails-go-to-test.goToTest', function () {
const editor = vscode.window.activeTextEditor
if (!editor) {
}
const document = editor.document
let fileName = document.fileName
let related = getRelated(fileName)
let relative = vscode.workspace.asRelativePath(related)
let fileExists = fs.existsSync(related)
let dirname = path.dirname(related)
//console.log('fileExists', fileExists);
if (fileExists) {
openFile(related)
} else {
prompt(relative, function () {
mkdirp.sync(dirname)
fs.writeFile(related, testContent(fileName), function (err) {
if (err) throw err
openFile(related)
})
})
}
});
context.subscriptions.push(disposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}