cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [09/13] cordova-browser git commit: run gets start_url from manifest, spec verifies manifest, Api delays writing to manifest and uses platform_www/
Date Sat, 17 Jun 2017 00:11:50 GMT
run gets start_url from manifest, spec verifies manifest, Api delays writing to manifest and
uses platform_www/


Project: http://git-wip-us.apache.org/repos/asf/cordova-browser/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-browser/commit/2e63f9fe
Tree: http://git-wip-us.apache.org/repos/asf/cordova-browser/tree/2e63f9fe
Diff: http://git-wip-us.apache.org/repos/asf/cordova-browser/diff/2e63f9fe

Branch: refs/heads/master
Commit: 2e63f9fe43d0e927f9d2ab9cfc2aedd1c2f74f36
Parents: c451180
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Wed Jun 14 16:23:58 2017 -0700
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Wed Jun 14 16:23:58 2017 -0700

----------------------------------------------------------------------
 bin/lib/create.js               |   9 +-
 bin/template/cordova/Api.js     | 184 ++++++++++++++++++++++-------------
 bin/template/cordova/lib/run.js |  41 +++++---
 spec/manifest.spec.js           |   5 +-
 4 files changed, 152 insertions(+), 87 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/2e63f9fe/bin/lib/create.js
----------------------------------------------------------------------
diff --git a/bin/lib/create.js b/bin/lib/create.js
index bdc79d6..bb6621f 100644
--- a/bin/lib/create.js
+++ b/bin/lib/create.js
@@ -78,8 +78,13 @@ module.exports.createProject = function(project_path,package_name,project_name){
              path.join(project_path,'platform_www'));
 
     //copy manifest file to platform_www
-    shell.cp(path.join(ROOT, 'bin/template/www', 'manifest.json'),
-             path.join(project_path,'platform_www'));
+    var platform_www = path.join(project_path,'platform_www');
+    // load manifest to write name/shortname
+    var manifest = require(path.join(ROOT, 'bin/template/www', 'manifest.json'));
+    manifest.name = project_name;
+    manifest.short_name = project_name;
+    fs.writeFileSync(path.join(project_path,'platform_www','manifest.json'),
+                     JSON.stringify(manifest, null, 2), 'utf-8');
 
     return Promise.resolve();
 };

http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/2e63f9fe/bin/template/cordova/Api.js
----------------------------------------------------------------------
diff --git a/bin/template/cordova/Api.js b/bin/template/cordova/Api.js
index 9f14ced..3b1ea8e 100644
--- a/bin/template/cordova/Api.js
+++ b/bin/template/cordova/Api.js
@@ -39,16 +39,17 @@ function setupEvents(externalEventEmitter) {
 function Api(platform, platformRootDir, events) {
 
     this.platform = platform || PLATFORM_NAME;
+
+    // MyApp/platforms/browser
     this.root = path.resolve(__dirname, '..');
     this.events = setupEvents(events);
-
     this.parser = new BrowserParser(this.root);
     this._handler = require('./browser_handler');
 
     this.locations = {
         platformRootDir: platformRootDir,
         root: this.root,
-        www: path.join(this.root, 'assets/www'),
+        www: path.join(this.root, 'www'),
         res: path.join(this.root, 'res'),
         platformWww: path.join(this.root, 'platform_www'),
         configXml: path.join(this.root, 'config.xml'),
@@ -65,12 +66,10 @@ function Api(platform, platformRootDir, events) {
 }
 
 Api.createPlatform = function (dest, config, options, events) {
-    // console.log("=======================");
-    // console.log("browser createPlatform !! dest:" + dest);
-    // console.log("options=",options);
-    // console.log("events="+events);
 
+    var creator = require('../../lib/create');
     events = setupEvents(events);
+
     var name = "HelloCordova";
     var id = "io.cordova.hellocordova";
     if(config) {
@@ -80,56 +79,20 @@ Api.createPlatform = function (dest, config, options, events) {
 
     var result;
     try {
-
-        var creator = require('../../lib/create');
+        // we create the project using our scripts in this platform
         result = creator.createProject(dest, id, name, options)
         .then(function () {
             // after platform is created we return Api instance based on new Api.js location
+            // Api.js has been copied to the new project
             // This is required to correctly resolve paths in the future api calls
             var PlatformApi = require(path.resolve(dest, 'cordova/Api'));
             return new PlatformApi('browser', dest, events);
         });
     }
     catch(e) {
-        console.log("error : " + e);
         events.emit('error','createPlatform is not callable from the browser project API.');
         throw(e);
     }
-
-    // Create manifest.json
-    var manifestJson = {};
-    var manifestJsonPath = path.join(dest,'www','manifest.json');
-
-    // Check if path exists and require manifestJsonPath.
-    if(fs.existsSync(manifestJsonPath)) {
-        try {
-            manifestJson = require(manifestJsonPath);
-        }
-        catch (e) {
-            console.log("error : " + e);
-            events.emit('error', 'unable to require manifest.json path.');
-        }
-    }
-
-    if(config){
-        if(config.name()) {
-            manifestJson.name = config.name();
-        }
-        if(config.shortName()) {
-            manifestJson.short_name = config.shortName();
-        }
-        if(config.packageName()) {
-            manifestJson.version = config.packageName();
-        }
-        if(config.description()) {
-            manifestJson.description = config.description();
-        }
-        if(config.author()) {
-            manifestJson.author = config.author();
-        }
-    }
-    fs.writeFileSync(manifestJsonPath, JSON.stringify(manifestJson, null, 4), 'utf8');
-
     return result;
 };
 
@@ -147,7 +110,7 @@ Api.prototype.getPlatformInfo = function () {
         "locations":this.locations,
         "root": this.root,
         "name": this.platform,
-        "version": { "version" : "1.0.0" },
+        "version": { "version" : "1.0.0" }, // um, todo!
         "projectConfig": this.config
     };
 };
@@ -155,44 +118,127 @@ Api.prototype.getPlatformInfo = function () {
 Api.prototype.prepare = function (cordovaProject,options) {
 
     // First cleanup current config and merge project's one into own
-    var defaultConfig = path.join(this.locations.platformRootDir,'cordova',
+    var defaultConfigPath = path.join(this.locations.platformRootDir,'cordova',
                         'defaults.xml');
-
-    var ownConfig = this.locations.configXml;
-
+    var ownConfigPath = this.locations.configXml;
     var sourceCfg = cordovaProject.projectConfig;
+
     // If defaults.xml is present, overwrite platform config.xml with it.
     // Otherwise save whatever is there as defaults so it can be
     // restored or copy project config into platform if none exists.
-    if (fs.existsSync(defaultConfig)) {
+    if (fs.existsSync(defaultConfigPath)) {
         this.events.emit('verbose', 'Generating config.xml from defaults for platform "'
+ this.platform + '"');
-        shell.cp('-f', defaultConfig, ownConfig);
-    } else if (fs.existsSync(ownConfig)) {
-        shell.cp('-f', ownConfig, defaultConfig);
-    } else {
-        shell.cp('-f', sourceCfg.path, ownConfig);
+        shell.cp('-f', defaultConfigPath, ownConfigPath);
+    }
+    else if (fs.existsSync(ownConfigPath)) {
+        this.events.emit('verbose', 'Generating defaults.xml from own config.xml for platform
"' + this.platform + '"');
+        shell.cp('-f', ownConfigPath, defaultConfigPath);
+    }
+    else {
+        this.events.emit('verbose', 'case 3"' + this.platform + '"');
+        shell.cp('-f', sourceCfg.path, ownConfigPath);
     }
 
-    // this._munger.reapply_global_munge().save_all();
-
-    this.config = new ConfigParser(ownConfig);
+    // merge our configs
+    this.config = new ConfigParser(ownConfigPath);
     xmlHelpers.mergeXml(cordovaProject.projectConfig.doc.getroot(),
-        this.config.doc.getroot(), this.platform, true);
+                        this.config.doc.getroot(),
+                        this.platform, true);
     this.config.write();
 
-    /*
-        "browser": {
-        "parser_file": "../cordova/metadata/browser_parser",
-        "handler_file": "../plugman/platforms/browser",
-        "url": "https://git-wip-us.apache.org/repos/asf?p=cordova-browser.git",
-        "version": "~4.1.0",
-        "deprecated": false
-    }
-    */
-
     // Update own www dir with project's www assets and plugins' assets and js-files
     this.parser.update_www(cordovaProject.locations.www);
 
+    // Copy or Create manifest.json
+    // todo: move this to a manifest helper module
+    // output path
+    var manifestPath = path.join(this.locations.www,'manifest.json');
+    var srcManifestPath =path.join(cordovaProject.locations.www,'manifest.json');
+    if(fs.existsSync(srcManifestPath)) {
+        // just blindly copy it to our output/www
+        // todo: validate it? ensure all properties we expect exist?
+        this.events.emit('verbose','copying ' + srcManifestPath + ' => ' + manifestPath);
+        shell.cp('-f',srcManifestPath,manifestPath);
+    }
+    else {
+        var manifestJson = {
+            "background_color": "#000",
+            "display": "standalone"
+        };
+        if(this.config){
+            if(this.config.name()) {
+                manifestJson.name = this.config.name();
+            }
+            if(this.config.shortName()) {
+                manifestJson.short_name = this.config.shortName();
+            }
+            if(this.config.packageName()) {
+                manifestJson.version = this.config.packageName();
+            }
+            if(this.config.description()) {
+                manifestJson.description = this.config.description();
+            }
+            if(this.config.author()) {
+                manifestJson.author = this.config.author();
+            }
+            // icons
+            var icons = this.config.getStaticResources('browser','icon');
+            var manifestIcons = icons.map(function(icon) {
+                // given a tag like this :
+                // <icon src="res/ios/icon.png" width="57" height="57" density="mdpi"
/>
+                /* configParser returns icons that look like this :
+                {   src: 'res/ios/icon.png',
+                    target: undefined,
+                    density: 'mdpi',
+                    platform: null,
+                    width: 57,
+                    height: 57
+                } ******/
+                /* manifest expects them to be like this :
+                {   "src": "images/touch/icon-128x128.png",
+                    "type": "image/png",
+                    "sizes": "128x128"
+                } ******/
+                // ?Is it worth looking at file extentions?
+                return {"src":icon.src, "type":"image/png",
+                        "sizes":(icon.width + "x" + icon.height)};
+            });
+            manifestJson.icons = manifestIcons;
+
+            // orientation
+            // <preference name="Orientation" value="landscape" />
+            var oriPref = this.config.getGlobalPreference('Orientation');
+            if(oriPref && ["landscape","portrait"].indexOf(oriPref) > -1) {
+                manifestJson.orientation = oriPref;
+            }
+
+            // get start_url
+            var contentNode = this.config.doc.find('content') || {'attrib':{'src':'index.html'}};
// sensible default
+            manifestJson.start_url = contentNode.attrib.src;
+
+            // now we get some values from start_url page ...
+            var startUrlPath = path.join(cordovaProject.locations.www,manifestJson.start_url);
+            if(fs.existsSync(startUrlPath)) {
+                var contents = fs.readFileSync(startUrlPath, 'utf-8');
+                // matches <meta name="theme-color" content="#FF0044">
+                var themeColorRegex = /<meta(?=[^>]*name="theme-color")\s[^>]*content="([^>]*)"/i;
+                var result = themeColorRegex.exec(contents);
+                var themeColor;
+                if(result && result.length>=2) {
+                    themeColor = result[1];
+                }
+                else { // see if there is a preference in config.xml
+                    // <preference name="StatusBarBackgroundColor" value="#000000" />
+                    themeColor = this.config.getGlobalPreference('StatusBarBackgroundColor');
+                }
+                if(themeColor) {
+                    manifestJson.theme_color = themeColor;
+                }
+            }
+        }
+        fs.writeFileSync(manifestPath, JSON.stringify(manifestJson, null, 2), 'utf8');
+    }
+
     // update project according to config.xml changes.
     return this.parser.update_project(this.config, options);
 };

http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/2e63f9fe/bin/template/cordova/lib/run.js
----------------------------------------------------------------------
diff --git a/bin/template/cordova/lib/run.js b/bin/template/cordova/lib/run.js
index 20878a9..d95984c 100644
--- a/bin/template/cordova/lib/run.js
+++ b/bin/template/cordova/lib/run.js
@@ -26,37 +26,52 @@ var fs = require('fs'),
 
 module.exports.run = function(args) {
     // defaults
-    console.log('here');
     args.port =   args.port || 8000;
     args.target = args.target || "chrome"; // make default the system browser
 
-    var root = path.join(__dirname, '../');
-    var configFilePath = path.resolve(path.join(root, 'config.xml'));
+    var wwwPath = path.join(__dirname,'../../www');
+    var configFilePath = path.resolve(path.join(wwwPath, 'config.xml'));
+    var manifestFilePath = path.resolve(path.join(wwwPath,'manifest.json'));
+
     var startPage;
-    if(fs.existsSync(configFilePath)) {
+
+    // get start page from manifest first, if available(it should always be)
+    // if not found, get it from config.xml
+    // if still not found, use default of 'index.html'
+
+    if(fs.existsSync(manifestFilePath)) {
+        try {
+            var manifest = require(manifestFilePath);
+            startPage = manifest.start_url;
+        }
+        catch(err) {
+            console.log("failed to require manifest ... " + err);
+        }
+    }
+
+    if(!startPage && fs.existsSync(configFilePath)) {
         var configXML = fs.readFileSync(configFilePath, 'utf8');
         // pull out <content src='SOME_URL'/>
-        startPage = /<content[\s]+?src\s*=\s*"(.*?)"/i.exec(configXML);
+        var result = /<content[\s]+?src\s*=\s*"(.*?)"/i.exec(configXML);
+        if(result) {
+            startPage = result[1];
+        }
     }
 
 
-
-
     var server = cordovaServe();
-
     server.servePlatform('browser', {port: args.port, noServerInfo: true})
     .then(function () {
         if(!startPage) {
+            // failing all else, set the default
             startPage = 'index.html';
         }
-        else {
-            startPage = startPage[1];
-        }
         var projectUrl = url.resolve('http://localhost:' + server.port + '/', startPage);
         console.log('startPage = ' + startPage);
         console.log('Static file server running @ ' + projectUrl + '\nCTRL + C to shut down');
-        return cordovaServe.launchBrowser({target: args.target, url: projectUrl});
-    }).catch(function (error) {
+        return cordovaServe.launchBrowser({"target": args.target, "url": projectUrl});
+    })
+    .catch(function (error) {
         console.log(error.message || error.toString());
         if (server.server) {
             server.server.close();

http://git-wip-us.apache.org/repos/asf/cordova-browser/blob/2e63f9fe/spec/manifest.spec.js
----------------------------------------------------------------------
diff --git a/spec/manifest.spec.js b/spec/manifest.spec.js
index 5afe204..e00db85 100644
--- a/spec/manifest.spec.js
+++ b/spec/manifest.spec.js
@@ -41,7 +41,7 @@ function createAndBuild(projectname, projectid) {
     return_code = shell.exec(command).code;
     expect(return_code).toBe(0);
 
-    var platWwwPath = path.join(tmpDir,projectname,"www");
+    var platWwwPath = path.join(tmpDir,projectname,"platform_www");
 
     var manifestPath = path.join(platWwwPath,'manifest.json');
     expect(fs.existsSync(manifestPath)).toBe(true);
@@ -68,8 +68,7 @@ function createAndBuild(projectname, projectid) {
     expect(manifestObj.icons.length).toBeDefined();
     expect(manifestObj.icons.length).toBeGreaterThan(0);
 
-
-    // related_applications[{platform:'web'},{platform:'play',url:...}]
+    // related_applications[{platform:'web'},{platform:'play',url:...}] ?
 
     // clean-up
     shell.rm('-rf', tmpDir);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message