Skip to content

Commit 1806f42

Browse files
tdusnokirobertsipka
authored andcommitted
Add Create IoTjs Tizen Project option to extension (#22)
IoT.js-VSCode-DCO-1.0-Signed-off-by: Tibor Dusnoki [email protected]
1 parent 6a872f4 commit 1806f42

File tree

4 files changed

+136
-18
lines changed

4 files changed

+136
-18
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ After installing Tizen Studio you can add the following lines of information to
205205
"IoTjsPath": "/absolute/path/to/iotjs"
206206
}
207207
```
208-
This enables the extension to install required packages for Tizen Studio to be able to create IoTjsApp native project. The installation may take several minutes.
208+
This enables the extension to install required packages for Tizen Studio to be able to create [IoTjsApp native project](#create-tizen-native-project). The installation may take several minutes.
209+
210+
# Create Tizen Native Project
211+
To create a Tizen Native Project in VSCode you need to simply click the 'Create IoTjs Tizen Project' button on the top right corner of your screen and provide information such as name and destination directory for your new IoTjsApp.
209212

210213
# License
211214
IoT.js VSCode extension is Open Source software under the [Apache 2.0 license](LICENSE). Complete license and copyright information can be found within the code.

package.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
],
5252
"scripts": {
5353
"vscode:prepublish": "npm run compile",
54-
"copy": "cp ./src/IotjsFunctions.json ./tools/setup.sh ./tools/InstallTizenApp.sh ./out/",
54+
"copy": "cp ./src/IotjsFunctions.json ./tools/setup.sh ./tools/InstallTizenApp.sh ./tools/CreateTizenProject.sh ./out/",
5555
"compile": "tsc -p ./ && npm run copy",
5656
"watch": "tsc -watch -p ./",
5757
"postinstall": "node ./node_modules/vscode/bin/install",
@@ -77,15 +77,29 @@
7777
"ws": "^5.1.1"
7878
},
7979
"activationEvents": [
80+
"*",
8081
"onLanguage:javascript",
8182
"onDebug",
82-
"onCommand:iotjs-debug.initialConfigurations"
83+
"onCommand:iotjs-debug.initialConfigurations",
84+
"onCommand:iotjs-debug.createTizenProject"
8385
],
8486
"main": "./out/extension.js",
8587
"files": [
8688
"out/**"
8789
],
8890
"contributes": {
91+
"commands":[ {
92+
"command": "iotjs-debug.createTizenProject",
93+
"title": "Create IoT.js Tizen Project"
94+
}
95+
],
96+
"menus": {
97+
"editor/title": [ {
98+
"command": "iotjs-debug.createTizenProject",
99+
"group": "navigation"
100+
}
101+
]
102+
},
89103
"breakpoints": [
90104
{
91105
"language": "javascript"

src/extension.ts

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ import * as Cp from 'child_process';
2424
// FIX ME: Change this require to a more consistent solution.
2525
// tslint:disable:no-var-requires
2626
const iotjs = require('./IotjsFunctions.json');
27+
let tizenStudioPath = undefined;
28+
let IoTjsPath = undefined;
29+
let lastModified = {
30+
filePath: undefined,
31+
mtime: undefined
32+
};
2733

2834
const defaultModules = [{
2935
link: 'process',
@@ -62,6 +68,58 @@ const provideInitialConfigurations = (): string => {
6268
].join('\n');
6369
};
6470

71+
const checkPath = () => {
72+
fs.stat(lastModified.filePath, async (err, stat) => {
73+
if (lastModified.mtime < stat.mtime) {
74+
await setup();
75+
}
76+
createTizenProject();
77+
});
78+
};
79+
80+
const createTizenProject = async() => {
81+
if (!tizenStudioPath) {
82+
vscode.window.showErrorMessage('Please specify Tizen Studio path in launch.json');
83+
return;
84+
}
85+
const createPath = path.join(__dirname, 'CreateTizenProject.sh');
86+
const projectName = await vscode.window.showInputBox({
87+
placeHolder: 'IoT.js Tizen project name',
88+
prompt: 'Name your new project please',
89+
ignoreFocusOut: true
90+
});
91+
if (projectName) {
92+
const projectPath = await vscode.window.showOpenDialog({
93+
openLabel: 'Set as destination directory',
94+
canSelectFiles: false,
95+
canSelectFolders: true,
96+
canSelectMany: false
97+
});
98+
if (projectPath) {
99+
const createProject = Cp.execFile(createPath, [projectName, projectPath[0].path, tizenStudioPath]);
100+
createProject.stdout.on('data', createLog => {
101+
console.log(createLog.toString());
102+
});
103+
createProject.stderr.on('data', errorLog => {
104+
console.log(errorLog.toString());
105+
});
106+
fs.mkdir(`${projectPath[0].path}/.vscode`, (err) => {
107+
if (err) {
108+
vscode.window.showErrorMessage(err.message);
109+
return;
110+
}
111+
});
112+
fs.copyFile(lastModified.filePath, `${projectPath[0].path}/.vscode/launch.json`, (err) => {
113+
if (err) {
114+
vscode.window.showErrorMessage(err.message);
115+
return;
116+
}
117+
});
118+
vscode.commands.executeCommand('vscode.openFolder', projectPath[0], true);
119+
}
120+
}
121+
};
122+
65123
const walkSync = (dir: string, filelist: string[] = []): string[] => {
66124
fs.readdirSync(dir).forEach(file => {
67125
filelist = fs.statSync(path.join(dir, file)).isDirectory()
@@ -183,32 +241,49 @@ const createHover = (document: vscode.TextDocument, position: vscode.Position):
183241
return new vscode.Hover(hoverContent);
184242
};
185243

186-
const setup = () => {
187-
vscode.workspace.findFiles('**/launch.json')
188-
.then(files => {
189-
const setup = require(files[0].path);
244+
const setup = async () => {
245+
await getPath();
246+
if (tizenStudioPath && IoTjsPath) {
190247
const setupPath = path.join(__dirname, 'setup.sh');
191-
let tizenStudioPath;
192-
let IoTjsPath;
193-
setup.configurations.forEach(i => {
194-
if (i.tizenStudioPath && i.IoTjsPath) {
248+
const setupScript = Cp.spawn(`source ${setupPath}`, [tizenStudioPath.toString(), IoTjsPath.toString()]);
249+
setupScript.stdout.on('data', setupLog => {
250+
console.log(setupLog.toString());
251+
});
252+
}
253+
};
254+
255+
const getPath = async () => {
256+
lastModified.filePath = undefined;
257+
tizenStudioPath = undefined;
258+
await vscode.workspace.findFiles('**/launch.json')
259+
.then(files => {
260+
lastModified.filePath = files[0].path;
261+
fs.stat(lastModified.filePath, (err, stats) => {
262+
lastModified.mtime = stats.mtime;
263+
});
264+
let config = require(lastModified.filePath);
265+
config.configurations.forEach(i => {
266+
if (i.tizenStudioPath) {
195267
tizenStudioPath = i.tizenStudioPath;
268+
}
269+
if (i.IoTjsPath) {
196270
IoTjsPath = i.IoTjsPath;
197271
}
198272
});
199-
if (tizenStudioPath && IoTjsPath) {
200-
const setupScript = Cp.spawn(`source ${setupPath}`, [tizenStudioPath.toString(), IoTjsPath.toString()]);
201-
setupScript.stdout.on('data', setupLog => {
202-
console.log(setupLog.toString());
203-
});
204-
}
273+
Object.keys(require.cache).forEach((id) => {
274+
if (/launch.json/.test(id)) {
275+
delete require.cache[id];
276+
}
277+
});
205278
});
279+
206280
};
207281

208282
export const activate = (context: vscode.ExtensionContext) => {
209-
setup();
283+
getPath();
210284
context.subscriptions.push(
211285
vscode.commands.registerCommand('iotjs-debug.provideInitialConfigurations', provideInitialConfigurations),
286+
vscode.commands.registerCommand('iotjs-debug.createTizenProject', checkPath),
212287
vscode.debug.onDidReceiveDebugSessionCustomEvent(e => processCustomEvent(e)),
213288
vscode.languages.registerCompletionItemProvider(JS_MODE, {
214289
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {

tools/CreateTizenProject.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
# Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# usage: CreateTizenProject.sh [projectName] [/path/to/destination/directory] [/path/to/tizen/studio]
18+
19+
[ "$#" -ne 3 ] && { echo "Usage: $0 <project-name> <destination-directory> <tizen-studio>"; exit 1; }
20+
21+
TIZEN_PROJECT=$1
22+
PROJECT_DIRECTORY=$2
23+
TIZEN_STUDIO=$3
24+
export PATH=$PATH:$TIZEN_STUDIO/tools/ide/bin
25+
tizen create native-project -p iot-headless-4.0 -t IoTjsApp -n $TIZEN_PROJECT -- $PROJECT_DIRECTORY
26+
tizen build-native -r iot-headless-4.0-device.core -- $PROJECT_DIRECTORY/$TIZEN_PROJECT

0 commit comments

Comments
 (0)