Skip to content

Commit

Permalink
version 0.4.2: handle deeper node_modules search and scopes
Browse files Browse the repository at this point in the history
Verify files exist in pod/gradle
Automatically resolve symlinks for iOS
  • Loading branch information
ericwlange committed Jan 31, 2019
1 parent 4a4292a commit e05271b
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 61 deletions.
107 changes: 75 additions & 32 deletions lib/gradle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const fs = require('fs')
const path = require('path')
const util = require('./util')

const liquidcore_version = '0.6.0'

Expand Down Expand Up @@ -46,16 +47,14 @@ const liquidcore_version = '0.6.0'
}
if (args.liquidcore && !fs.existsSync(path.resolve(args.liquidcore))) {
console.error('LiquidCore project cannot be found at ' + args.liquidcore);
exit(-2)
process.exit(-2)
}

let node_modules = path.resolve('.', 'node_modules')
let modules = fs.readdirSync(node_modules)
let build_gradle =
"dependencies {\n"
"dependencies {\n" +
" /* LiquidCore */\n" +
" if (findProject(':LiquidCoreAndroid') != null) {\n" +
" implementation project(':LiquidCoreAndroid')\n" +
" if (findProject(':LiquidCore') != null) {\n" +
" implementation project(':LiquidCore')\n" +
" } else {\n" +
" implementation 'com.github.LiquidPlayer:LiquidCore:"+liquidcore_version+"'\n" +
" }\n\n"
Expand All @@ -64,37 +63,81 @@ const liquidcore_version = '0.6.0'
if (args.liquidcore) {
settings_gradle +=
"/* LiquidCore */\n" +
"include ':LiquidCoreAndroid'\n" +
"project(':LiquidCoreAndroid').projectDir = new File(\n" +
"include ':LiquidCore'\n" +
"project(':LiquidCore').projectDir = new File(\n" +
" rootProject.projectDir, '"+args.liquidcore+"')\n\n"
}
modules.forEach(m => {
try {
let package = JSON.parse(fs.readFileSync(path.resolve(node_modules, m, 'package.json')))
let addon = package && package['liquidcore-addon']
let android = addon && addon.android
let android_dev = addon && addon['android-dev']
if (android || android_dev) {
build_gradle +=
" /* AddOn: "+ m +" */\n" +
" if (findProject(':"+m+"') != null) {\n" +
" implementation project(':"+m+"')\n"
if (android) {
build_gradle +=
" } else {\n" +
" implementation(path:new File(rootProject.projectDir, 'node_modules/"+m+"/"+android+"'), ext:'aar')\n"

let modules = []
util.recurse_packages('.', 'node_modules', (package, resolved) => {
let addon = package['liquidcore-addon']
let android = addon && addon.android
let android_dev = addon && addon['android-dev']
if (android || android_dev) {
let module = { name: package.name, module: path.basename(resolved) }
if (android) {
let aar = path.resolve(resolved, android + '.aar')
try {
fs.realpathSync(aar)
module.android = path.relative(path.resolve('.'), aar)
} catch (e) {
console.warn("WARN: '%s' does not exist, skipping", aar)
}
build_gradle +=
" }\n\n"
if (!android || (args.dev && android_dev)) {
settings_gradle +=
"/* AddOn: "+m+" */\n" +
"include ':"+m+"'\n" +
"project(':"+m+"').projectDir = new File(\n" +
" rootProject.projectDir, 'node_modules/"+m+"/"+android_dev+"')\n\n"
}
if (android_dev) {
let pth = path.resolve(resolved, android_dev)
try {
fs.realpathSync(pth)
module.android_dev = path.relative(path.resolve('.'), pth)
} catch (e) {
console.warn("WARN: Project directory '%s' does not exist, skipping", pth)
}
}
} catch (e) {}
if (module.android || module.android_dev) {
modules.push(module)
}
}
})

// Clean up and de-dupe modules
// Precedence rules:
// 1. dev > release
// 2. relative path closest to root wins
clean_modules = {}
modules.forEach(p => {
let current = clean_modules[p.name]
if (current !== undefined) {
if (!p.android_dev && current.android_dev) return // Rule 1
;if (current.android_dev && current.android_dev.length < p.android_dev.length) return // Rule 2
;if (current.android && !p.android_dev && (!p.android || current.android.length < p.android.length)) return
}
clean_modules[p.name] = p
})

Object.keys(clean_modules).forEach(k => {
let android = clean_modules[k].android
let android_dev = clean_modules[k].android_dev
let m = clean_modules[k].module
if (android || android_dev) {
build_gradle +=
" /* AddOn: "+ m +" */\n" +
" if (findProject(':"+m+"') != null) {\n" +
" implementation project(':"+m+"')\n"
if (android) {
build_gradle +=
" } else {\n" +
" implementation(path:new File(rootProject.projectDir, '"+android+"'), ext:'aar')\n"
}
build_gradle +=
" }\n\n"
if (!android || (args.dev && android_dev)) {
settings_gradle +=
"/* AddOn: "+m+" */\n" +
"include ':"+m+"'\n" +
"project(':"+m+"').projectDir = new File(\n" +
" rootProject.projectDir, '"+android_dev+"')\n\n"
}
}
})

build_gradle += '}\n'
Expand Down
2 changes: 1 addition & 1 deletion lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const path = require('path');
},
dependencies: {
"react-native": "0.56.0",
"liquidcore-cli": "^0.4.1",
"liquidcore-cli": "^0.4.2",
"babel-preset-env": "^1.7.0",
"babel-polyfill": "^6.26.0"
}
Expand Down
83 changes: 57 additions & 26 deletions lib/pod.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const fs = require('fs')
const path = require('path')
const util = require('./util')

const liquidcore_version = '0.6.0'
const default_ios_target_version = '10.0'
Expand All @@ -14,7 +15,7 @@ const Specs = 'https://github.com/LiquidPlayer/Specs.git'
;(() => {
const pod = (override) => {
var args = {
'ios-version' : default_ios_target_version,
iosVersion : default_ios_target_version,
version : liquidcore_version,
specs : Specs
}
Expand Down Expand Up @@ -64,52 +65,82 @@ const Specs = 'https://github.com/LiquidPlayer/Specs.git'
process.exit(-2)
}

let node_modules = path.resolve('.', 'node_modules')
let modules = fs.readdirSync(node_modules)
let podfile =
"platform :ios, '10.0'\n" +
"platform :ios, '"+args.iosVersion+"'\n" +
"use_frameworks!\n\n"
let specs = ['https://github.com/CocoaPods/Specs.git']
let pods = []

if (args.liquidcore) {
pods.push(" pod 'LiquidCore', :path => '"+args.liquidcore+"'")
pods.push({name : "LiquidCore", path : args.liquidcore })
} else {
pods.push(" pod 'LiquidCore, '" + args.version + "'")
if (!specs.includes(args.specs)) specs.unshift(args.specs)
pods.push({name : "LiquidCore", version: args.version, specs: args.specs})
}

modules.forEach(m => {
try {
let package = JSON.parse(fs.readFileSync(path.resolve(node_modules, m, 'package.json')))
let addon = package && package['liquidcore-addon']
let ios = addon && addon.ios
let ios_dev = addon && addon['ios-dev']
if (ios || ios_dev) {
if (!ios || (ios_dev && args.dev)) {
pods.push(" pod '"+ios_dev.name+"', :path => './node_modules/"+m+"/"+ios_dev.path+"'")
} else {
let p = " pod '"+ios.name+"'"
if (ios.version) {
p += ", '" + ios.version + "'"
}
pods.push(p)
if (ios.specs && !specs.includes(ios.specs)) specs.unshift(ios.specs)
util.recurse_packages('.', 'node_modules', (package, resolved) => {
let addon = package['liquidcore-addon']
let ios = addon && addon.ios
let ios_dev = addon && addon['ios-dev']
if (ios || ios_dev) {
if (!ios || (ios_dev && args.dev)) {
let ipath = path.resolve(resolved, ios_dev.path)
try {
// Cocoapods cannot handle symlinks and most annoyingly just kind of silently
// fails, so unwind symlinks
ios_dev.path = path.relative(path.resolve('.'), fs.realpathSync(ipath))
pods.push(ios_dev)
} catch (e) {
console.warn("WARN: '%s' does not exist, skipping", ipath)
}
} else {
pods.push(ios)
}
} catch (e) {}
}
})

// Clean up and de-dupe pods
// Precedence rules:
// 1. Dev pods > Release pods
// 2. Dev pods: relative path closest to root wins
// 3. Release pods: Highest version wins (FIXME: Need to deal with compatibility rules)
clean_pods = {}
pods.forEach(p => {
let current = clean_pods[p.name]
if (current !== undefined) {
if (current.path && !p.path) return // Rule 1
;if (current.path && current.path.length < p.path.length) return // Rule 2
;if (current.version && p.version && util.version(current.version) < util.version(p.version)) return // Rule 3
;if (!p.version) return
}
clean_pods[p.name] = p
})

// Include 'source' lines
Object.keys(clean_pods).forEach(k =>
clean_pods[k].specs &&
!specs.includes(clean_pods[k].specs) &&
specs.unshift(clean_pods[k].specs))
if (specs.length > 1) {
podfile += "source '"
podfile += specs.join("'\nsource '")
podfile += "'\n\n"
}

podfile += "target '"+args._[0]+"' do\n"
podfile += pods.join('\n')

podfile += "\nend\n"
// Include 'pod' lines
Object.keys(clean_pods).forEach(k => {
let p = clean_pods[k]
podfile += " pod '" + p.name + "'"
if (p.path) {
podfile += ", :path => '" + p.path + "'"
} else if (p.version) {
podfile += ", '" + p.version + "'"
}
podfile += '\n'
})

podfile += "end\n"

console.log(podfile)
}
Expand Down
39 changes: 39 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs')
const path = require('path')

;(() => {
let resolved_paths = []

function process(p, file, found) {
let resolved = path.resolve(p, file)
let real = fs.realpathSync(resolved)
if (resolved_paths.includes(real)) return; // Don't get stuck in an endless loop
resolved_paths.push(real)

try {
const stat = fs.statSync(resolved)
if (stat.isDirectory()) {
if (file == 'node_modules' || file[0] == '@') {
return fs.readdirSync(resolved).forEach(m => process(resolved, m, found))
} else {
try {
let package = JSON.parse(fs.readFileSync(path.resolve(resolved, 'package.json')))
let addon = package && package['liquidcore-addon']
if (addon) {
found(package, resolved)
}
return fs.readdirSync(resolved).forEach(m => process(resolved, m, found))
} catch (e) {}
}
}
} catch (e) {}
}

const version = (s) => s.split('.').map((v,n) =>
(s.length-n-1)*100*parseInt(v)).reduce((p,v)=>Number.isInteger(v)?p+v:v)

module.exports = {
recurse_packages : process,
version : version
}
})()
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "liquidcore-cli",
"version": "0.4.1",
"version": "0.4.2",
"description": "Command line utilities for LiquidCore",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e05271b

Please sign in to comment.