Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constants support for manifest file #63

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 6 additions & 2 deletions bin/ukor-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ program
'Specify a roku. Ignored if passed as argument.'
)
.option('-a, --auth <user:pass>', 'Set username and password for roku.')
.option('-p, --packageConfig <packageConfigName>', 'Specify package config name to be used to get packageReference and packageKey')
.parse(process.argv)

let args = program.args
Expand All @@ -31,15 +32,18 @@ try {
process.exit(-1)
}

let packageConfig = program.packageConfig || 'main'

let options = {
flavor,
roku,
auth,
name: ''
name: '',
packageConfig
}
for (let key in options) {
if (!options[key] && key != 'name') {
log.error('%s options is undefined')
log.error(key + ' option is undefined')
log.pretty('error', 'options:', options)
process.exit(-1)
}
Expand Down
6 changes: 5 additions & 1 deletion bin/ukor-rekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ program
'Specify a roku. Ignored if passed as argument.'
)
.option('-a, --auth <user:pass>', 'Set username and password for roku.')
.option('-p, --packageConfig <packageConfigName>', 'Specify package config name to be used to get packageReference and packageKey')
.parse(process.argv)

let args = program.args
Expand All @@ -32,11 +33,14 @@ try {
process.exit(-1)
}

let packageConfig = program.packageConfig || 'main'

let options = {
flavor: 'main',
roku,
auth,
name: ''
name: '',
packageConfig
}
for (let key in options) {
if (!options[key] && key != 'name') {
Expand Down
4 changes: 2 additions & 2 deletions bin/ukor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ program
'Bundle then deploy your channel to a named roku'
)
.command(
'package <flavor> <roku>',
'package <flavor> <roku> [-p, --packageConfig]',
'Package a channel flavor with a roku device'
)
.command(
'rekey <roku>',
'rekey <roku> [-p, --packageConfig]',
'Rekey your device (an packageReference is required)'
)
.command(
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const properties = require('../utils/properties')
function upload(options, ip, callback) {
const zip = path.join(
properties.buildDir,
options.flavor + options.name + '.zip'
utils.getPackageFileName(options) + '.zip'
)
const form = {
mysubmit: 'replace',
Expand Down
7 changes: 4 additions & 3 deletions lib/commands/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mkdirp = require('mkdirp')
const fse = require('fs-extra')
const rimraf = require('rimraf')
const inserter = require('../utils/inserter')
const utils = require('../utils/utils')
const childProcess = require('child_process')
const temp = '.ukor'

Expand Down Expand Up @@ -49,11 +50,11 @@ function bundleFlavor(options, callback) {
const build = options.build || properties.buildDir
const include = options.include || []
flavorSrc = include.concat(flavorSrc)
let name = options.name
if (!name) name = ''

let name = utils.getPackageFileName(options)
rimraf.sync(temp)
log.info('make flavor: ' + flavor)
let zip = flavor + name + '.zip'
let zip = name + '.zip'
mkdirp(build)
var out = fs.createWriteStream(path.join(build, zip))
var archive = archiver('zip')
Expand Down
30 changes: 18 additions & 12 deletions lib/commands/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ const path = require('path')
const request = require('request')
const log = require('../utils/log')
const properties = require('../utils/properties')
const utils = require('../utils/utils')

function create(options, ip) {
const curTime = new Date().getTime();

const zip = path.join(
properties.buildDir,
options.flavor + options.name + '.zip'
)
var packageKeyOverride = properties[options.packageConfig + ".packageKey"]
var packageKey = packageKeyOverride || properties.packageKey

if (packageKey == null) {
log.error("packageKey field is missing (ukor.properties)")
return
}

const form = {
app_name: getAppName(options),
mysubmit: 'Package',
pkg_time: curTime,
passwd: properties.packageKey
passwd: packageKey
}

request.post(
Expand Down Expand Up @@ -46,9 +51,11 @@ function create(options, ip) {
}

function download(options, ip, packageName) {
let downloadUrl = 'http://' + ip + '/' + packageName
log.verbose('Downloading package from: '.concat(downloadUrl))
request.get(
{
url: 'http://' + ip + '/' + packageName,
url: downloadUrl,
auth: options.auth
},
(error, response, body) => {
Expand All @@ -67,16 +74,15 @@ function download(options, ip, packageName) {
function getPackagePath(options) {
return [
properties.buildDir,
options.flavor
utils.getPackageFileName(options)
].join('/').concat('.pkg')
}

function getPackageName(urls) {
let result = null

urls.map(function(val) {
result = val.split('"')[1];
})
if (urls != null) {
result = urls[1]
}

return result
}
Expand All @@ -89,7 +95,7 @@ function getAppName(options) {
}

function parseBody(body) {
return body.match(/<a href=(.*?)>/g)
return body.match(/"pkgPath":"(.*?)"/)
}

module.exports = {
Expand Down
19 changes: 15 additions & 4 deletions lib/commands/rekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ const log = require('../utils/log')
const properties = require('../utils/properties')

function rekey(options, ip) {
if (properties.packageReference == null) {
var packageReferenceOverride = properties[options.packageConfig + ".packageReference"]
var packageReference = packageReferenceOverride || properties.packageReference

if (packageReference == null) {
log.error("packageReference field is missing (ukor.properties)")
return
}

var packageKeyOverride = properties[options.packageConfig + ".packageKey"]
var packageKey = packageKeyOverride || properties.packageKey

if (packageKey == null) {
log.error("packageKey field is missing (ukor.properties)")
return
}

const form = {
mysubmit: 'Rekey',
passwd: properties.packageKey,
archive: fs.createReadStream(properties.packageReference)
passwd: packageKey,
archive: fs.createReadStream(packageReference)
}

options.auth.sendImmediately = false
Expand All @@ -30,7 +41,7 @@ function rekey(options, ip) {
result = parseBody(body)

if (result[0].indexOf('Success') !== -1) {
log.info(`The device was rekeyed with: ${properties.packageKey}`)
log.info(`The device was rekeyed with: ${packageKey}`)
} else {
log.error('Falied to rekey');
}
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ function compile(directory, constants) {

fs.writeFileSync(file, src)
})

let manifestFile = utils.getManifestFile(directory)
if (manifestFile != null) {
let manifestData = fs.readFileSync(manifestFile)
let manifest = insertConstants(manifestData.toString(), constants, manifestFile)
fs.writeFileSync(manifestFile, manifest)
}
}

function mergeConstants(flavors) {
Expand Down
74 changes: 73 additions & 1 deletion lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const matchers = {
ip: /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,
usn: /^[A-Z0-9]{12}$/
}
const gitBranch = require('git-branch')
const os = require('os')

const temp = '.ukor'

function getAllSourceFiles(dir) {
let src = []
Expand All @@ -26,6 +30,21 @@ function getAllSourceFiles(dir) {
return src
}

function getManifestFile(dir) {
let manifestFilePath = path.join(dir, "manifest")
try {
let stats = fs.statSync(manifestFilePath)
if (stats.isFile()) {
return manifestFilePath
} else {
return null
}
} catch(err) {
log.warn('Manifest file not found')
return null
}
}

function parseRoku(arg){
if (Object.keys(properties.rokus).includes(arg)) {
return 'name'
Expand Down Expand Up @@ -70,11 +89,64 @@ function getUsn(options) {
return (parseRoku(options.roku) == 'name') ? properties.rokus[options.roku].serial : options.roku
}

function getVersionFromManifest(options) {
var result = {
majorVersion: '',
minorVersion: '',
buildVersion: ''
}

let manifestFile = getManifestFile(temp)
let manifest = fs.readFileSync(manifestFile).toString()
manifest.split(os.EOL).forEach(line => {
let keyValuePair = line.split('=')
if (keyValuePair[0] == 'major_version') {
result.majorVersion = keyValuePair[1].trim()
} else if (keyValuePair[0] == 'minor_version') {
result.minorVersion = keyValuePair[1].trim()
} else if (keyValuePair[0] == 'build_version') {
result.buildVersion = keyValuePair[1].trim()
}
})
return result
}

function getVersionStringFromManifest(options) {
let version = getVersionFromManifest(options)
return version.majorVersion + '.' + version.minorVersion + '.' + version.buildVersion
}

function getCurrentGitBranch() {
try {
return gitBranch.sync()
} catch (e) {
log.warn('Cannot get current git branch!')
return 'nobranch'
}
}

function getPackageFileName(options) {
var packageFileName = properties[options.packageConfig + ".packageFileName"] || properties["packageFileName"] || options.flavor
let version = getVersionStringFromManifest(options)
let gitBranch = getCurrentGitBranch()

packageFileName = packageFileName.replace('%flavor%', options.flavor)
packageFileName = packageFileName.replace('%version%', version)
packageFileName = packageFileName.replace('%branch%', gitBranch)

return packageFileName
}

module.exports = {
parseRoku,
parseAuth,
continueIfExists,
getAllSourceFiles,
getManifestFile,
getDeviceInfo,
getUsn
getUsn,
getVersionFromManifest,
getVersionStringFromManifest,
getCurrentGitBranch,
getPackageFileName
}
Loading