Skip to content

Commit

Permalink
Add api v1.2.0 (alpha), Fix objects in 1.0/1.1, add update command fo…
Browse files Browse the repository at this point in the history
…r updating api
  • Loading branch information
itslenny committed Aug 5, 2016
1 parent ddb1d44 commit 608991c
Show file tree
Hide file tree
Showing 20 changed files with 3,201 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Refer to our [documentation repository](https://github.com/Microsoft/PowerBI-vis

##Usage

Learn more about using these tools in the [Usage Guide](https://github.com/Microsoft/PowerBI-visuals-docs/tree/master/tools)
Learn more about using these tools in the [Installation Guide](https://github.com/Microsoft/PowerBI-visuals-docs/tree/master/tools) and [Usage Guide](https://github.com/Microsoft/PowerBI-visuals-docs/blob/master/tools/usage.md)

##[Roadmap](https://github.com/Microsoft/PowerBI-visuals-docs/blob/master/Roadmap/README.md)

Expand Down
13 changes: 12 additions & 1 deletion bin/pbiviz-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
let path = require('path');
let program = require('commander');
let VisualPackage = require('../lib/VisualPackage');
let VisualGenerator = require('../lib/VisualGenerator');
let ConsoleWriter = require('../lib/ConsoleWriter');

program
.option('-f, --force', 'force creation (overwrites folder if exists)')
.option('-t, --template [template]', 'use a specific template (default, table)')
.option('--api-version [version]', 'use a specific api version (1.0.0, 1.1.0, 1.2.0, ...)')
.parse(process.argv);

let args = program.args;
Expand All @@ -43,6 +45,14 @@ if (!args || args.length < 1) {
process.exit(1);
}

//pre-check API version before we create anything
if (program.apiVersion) {
if (!VisualGenerator.checkApiVersion(program.apiVersion)) {
ConsoleWriter.error('Invalid API version: ' + program.apiVersion);
process.exit(1);
}
}

let visualName = args.join(' ');
let cwd = process.cwd();

Expand All @@ -54,7 +64,8 @@ if (program.force) {

let generateOptions = {
force: program.force,
template: program.template
template: program.template,
apiVersion: program.apiVersion
};

VisualPackage.createVisualPackage(cwd, visualName, generateOptions).then(() => {
Expand Down
55 changes: 55 additions & 0 deletions bin/pbiviz-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

/*
* Power BI Visual CLI
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the ""Software""), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

"use strict";

let program = require('commander');
let VisualPackage = require('../lib/VisualPackage');
let VisualGenerator = require('../lib/VisualGenerator');
let ConsoleWriter = require('../lib/ConsoleWriter');
let fs = require('fs');

program.parse(process.argv);

let args = program.args;

let cwd = process.cwd();

VisualPackage.loadVisualPackage(cwd).then((visualPackage) => {
let apiVersion = args.length > 0 ? args[0] : visualPackage.config.apiVersion;
let visualPath = visualPackage.buildPath();
VisualGenerator.updateApi(visualPath, apiVersion)
.then(() => visualPackage.config.apiVersion === apiVersion ? false : VisualGenerator.setApiVersion(visualPath, apiVersion))
.then(() => ConsoleWriter.info(`Visual api ${apiVersion} updated`))
.catch(e => {
ConsoleWriter.error('UPDATE ERROR', e);
process.exit(1);
});
}).catch((e) => {
ConsoleWriter.error('LOAD ERROR', e);
process.exit(1);
});
1 change: 1 addition & 0 deletions bin/pbiviz.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ program
.command('info', 'Display info about the current visual')
.command('start', 'Start the current visual')
.command('package', 'Package the current visual into a pbiviz file')
.command('update [version]', 'Updates the api definitions and schemas in the current visual. Changes the version if specified')
.option('--install-cert', 'Install localhost certificate', openCertFile);

//prepend logo to help screen
Expand Down
5 changes: 4 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"templates": {
"visual": "templates/visual",
"visuals": "templates/visuals",
"pbiviz": "templates/pbiviz.json.template",
"plugin": "templates/plugin.ts.template",
"package": "templates/package.json.template"
},
"generate": {
"apiVersion": "1.1.0"
},
"build": {
"precompileFolder": ".tmp/precompile",
"dropFolder": ".tmp/drop",
Expand Down
11 changes: 7 additions & 4 deletions lib/VisualBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ class VisualBuilder extends EventEmitter {
try {
fs.accessSync(visualPackage.buildPath('.api', version));
} catch (e) {
return reject([{
type: 'validation',
message: 'Invalid API Version ' + version
}]);
if (e && e.code && e.code === 'ENOENT') {
return reject([{
type: 'validation',
message: 'Invalid API Version ' + version
}]);
}
reject(e);
}

//check that tsconfig is referencing the correct version
Expand Down
97 changes: 89 additions & 8 deletions lib/VisualGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ let fs = require('fs-extra');
let path = require('path');
let uuid = require('node-uuid');
let _ = require('lodash');
let config = require('../config.json');

const VISUAL_TEMPLATES_PATH = path.resolve(__dirname + '/../templates/visuals');
const VISUAL_TEMPLATES_PATH = path.join(__dirname, '..', config.templates.visuals);
const GUID_PREFIX = 'PBI-CV-';
const PBIVIZ_TEMPLATE_PATH = path.resolve(__dirname + '/../templates/pbiviz.json.template');
const PBIVIZ_TEMPLATE_PATH = path.resolve(__dirname, '..', config.templates.pbiviz);
const API_VERSION = config.generate.apiVersion;

/**
* Generates a random GUID for your visual
Expand All @@ -46,13 +48,13 @@ function generateVisualGuid() {
/**
* Generates the data for the visual
*/
function generateVisualOptions(visualName) {
function generateVisualOptions(visualName, apiVersion) {
return {
name: generateVisualName(visualName),
displayName: visualName,
guid: generateVisualGuid(),
visualClassName: 'Visual',
apiVersion: '1.1.0',
apiVersion: apiVersion
};
}

Expand Down Expand Up @@ -105,7 +107,8 @@ function validTemplate(templateName) {

const defaultOptions = {
force: false,
template: 'default'
template: 'default',
apiVersion: API_VERSION
};

class VisualGenerator {
Expand All @@ -120,8 +123,7 @@ class VisualGenerator {
static generate(targetPath, visualName, options) {
return new Promise((resolve, reject) => {
let buildOptions = _.defaults(options, defaultOptions);

let visualOptions = generateVisualOptions(visualName);
let visualOptions = generateVisualOptions(visualName, buildOptions.apiVersion);
if (!visualOptions || !visualOptions.name) {
return reject(new Error('Invalid visual name'));
}
Expand All @@ -142,13 +144,92 @@ class VisualGenerator {

copyVisualTemplate(visualPath, buildOptions.template);
createPbiVizJson(visualPath, visualOptions);
resolve(visualPath);
VisualGenerator.updateApi(visualPath, visualOptions.apiVersion)
.then(() => VisualGenerator.setApiVersion(visualPath, visualOptions.apiVersion))
.then(() => resolve(visualPath))
.catch(reject);
} catch (e) {
reject(e);
}
});
});
}

/**
* Copies the api folder for the specified api version
*
* @param {string} targetPath - file path of the root of the visual package
* @param {string} apiVersion - api version to update
*/
static updateApi(targetPath, apiVersion) {
return new Promise((resolve, reject) => {
let version = 'v' + apiVersion;
let apiSourcePath = path.join(VISUAL_TEMPLATES_PATH, '.api', version);
let apiTargetPath = path.join(targetPath, '.api', version);

if (!VisualGenerator.checkApiVersion(apiVersion)) {
return reject(new Error('Invalid API version: ' + apiVersion));
}

fs.removeSync(apiTargetPath);
fs.ensureDirSync(apiTargetPath);
fs.copySync(apiSourcePath, apiTargetPath);
resolve();
});
}

/**
* Sets the api version for a visual (tsconfig.json, pbiviz.json, .vscode/settings.json)
*
* @param {string} targetPath - file path of the root of the visual package
* @param {string} apiVersion - api version to set
*/
static setApiVersion(targetPath, apiVersion) {
let pbivizPath = path.join(targetPath, 'pbiviz.json');
let tsConfigPath = path.join(targetPath, 'tsconfig.json');
let vsCodeSettingsPath = path.join(targetPath, '.vscode', 'settings.json');

//set version in pbiviz.json
let pbiviz = fs.readJsonSync(pbivizPath);
pbiviz.apiVersion = apiVersion;
fs.writeJsonSync(pbivizPath, pbiviz);

//set correct version d.ts file in tsconfig.json
let tsConfig = fs.readJsonSync(tsConfigPath);
let typeDefIndex = _.findIndex(tsConfig.files, i => i.match(/.api\/.+\/PowerBI-visuals.d.ts$/));
tsConfig.files[typeDefIndex] = `.api/v${apiVersion}/PowerBI-visuals.d.ts`;
fs.writeJsonSync(tsConfigPath, tsConfig);

//set correct version schemas in .vscode/settings.json
let vsCodeSettings = fs.readJsonSync(vsCodeSettingsPath);
vsCodeSettings['json.schemas'].forEach((item, idx) => {
if (item.url.match(/.api\/.+\/schema.pbiviz.json$/)) {
vsCodeSettings['json.schemas'][idx].url = `./.api/v${apiVersion}/schema.pbiviz.json`;
} else if (item.url.match(/.api\/.+\/schema.capabilities.json$/)) {
vsCodeSettings['json.schemas'][idx].url = `./.api/v${apiVersion}/schema.capabilities.json`;
}
});
fs.writeJsonSync(vsCodeSettingsPath, vsCodeSettings);
}

/**
* Checks an if a specified API version is valid
*
* @param {string} apiVersion - api version to set
* @returns {boolean} - is this version valid
*/
static checkApiVersion(apiVersion) {
let apiSourcePath = path.join(VISUAL_TEMPLATES_PATH, '.api', 'v' + apiVersion);
try {
fs.accessSync(apiSourcePath);
} catch (e) {
if (e && e.code && e.code === 'ENOENT') {
return false;
}
throw e;
}
return true;
}
}

module.exports = VisualGenerator;
12 changes: 6 additions & 6 deletions lib/VisualPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ class VisualPackage {
* Creates a new visual package
*
* @param {string} rootPath - file path to root of visual package
* @param {boolean} [force=false] - overwrites existing folder if set to true
* @param {string} visualName - name of the visual to create in the rootPath
* @param {object} generateOptions - options for the visual generator
* <VisualPackage>} - instance of newly created visual package
*/
static createVisualPackage(rootPath, visualName, force) {
static createVisualPackage(rootPath, visualName, generateOptions) {
return new Promise((resolve, reject) => {
VisualGenerator.generate(rootPath, visualName, force).then((visualPath) => {
VisualGenerator.generate(rootPath, visualName, generateOptions).then((visualPath) => {
VisualPackage.loadVisualPackage(visualPath).then(resolve).catch(reject);
}).catch(reject);
});
Expand All @@ -62,11 +63,10 @@ class VisualPackage {
try {
resolve(new VisualPackage(rootPath));
} catch (e) {
let error = e;
if (e && e.code && e.code === 'ENOENT') {
error = new Error(CONFIG_FILE + ' not found. You must be in the root of a visual project to run this command.');
return reject(new Error(CONFIG_FILE + ' not found. You must be in the root of a visual project to run this command.'));
}
reject(error);
reject(e);
}
});
}
Expand Down

0 comments on commit 608991c

Please sign in to comment.