You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by jasongin <gi...@git.apache.org> on 2016/04/19 19:49:36 UTC

[GitHub] cordova-lib pull request: Add FileUpdater module to cordova-common

GitHub user jasongin opened a pull request:

    https://github.com/apache/cordova-lib/pull/429

    Add FileUpdater module to cordova-common

    The new FileUpdater module contains a few functions that enable optimized
    file copies by comparing timestamps. These functions are primarily
    intended to be used by each platform's prepare operation to avoid
    redundantly copying files that haven't changed since the last build, thus
    greatly speeding up build times after the first build. But the usfulness
    isn't necessarily limited to the prepare operation; the functions could
    be used for any Cordova file copy operations that benefit from the same
    optimization.
    
    In this initial change, the FileUpdater isn't used yet anywhere. Look for
    follow-up changes in other repos which make use of the FileUpdater from
    platforms' prepare operations, and a CLI change that enables the
    timestamp-based optimization via a new --incremental flag.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/jasongin/cordova-lib master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/cordova-lib/pull/429.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #429
    
----
commit 542dd2d57d94829869a1cddbe0ff7b04ed4517df
Author: Jason Ginchereau <ja...@microsoft.com>
Date:   2016-04-16T02:27:01Z

    Add FileUpdater module to cordova-common
    
    The new FileUpdater module contains a few functions that enable optimized
    file copies by comparing timestamps. These functions are primarily
    intended to be used by each platform's prepare operation to avoid
    redundantly copying files that haven't changed since the last build, thus
    greatly speeding up build times after the first build. But the usfulness
    isn't necessarily limited to the prepare operation; the functions could
    be used for any Cordova file copy operations that benefit from the same
    optimization.
    
    In this initial change, the FileUpdater isn't used yet anywhere. Look for
    follow-up changes in other repos which make use of the FileUpdater from
    platforms' prepare operations, and a CLI change that enables the
    timestamp-based optimization via a new --incremental flag.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: Add FileUpdater module to cordova-common

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-212121255
  
    I opened CB-11117 to track this work.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60481666
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    --- End diff --
    
    Because it was the first parameter and later parameters are required, it could not be optional. But it will become optional (actually an optional part of an optional options object) after I rearrange the parameters.
    
    ---
    In reply to: [60481370](https://github.com/apache/cordova-lib/pull/429#discussion_r60481370) [](ancestors = 60481370)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: Add FileUpdater module to cordova-common

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-212039256
  
    @nikhilkh @vladimir-kotikov please review


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by purplecabbage <gi...@git.apache.org>.
Github user purplecabbage commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216614096
  
    I would not consider that contrived. 
    This would be an extremely tough issue to find if it happened. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60396872
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    --- End diff --
    
    Same as above


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-217236642
  
    @nikhilkh waiting on you. Thanks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60459967
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    --- End diff --
    
    No. Actually the code just before line 72 may set targetStats to null. That's why line 75 is not an "else".
    
    ---
    In reply to: [60398288](https://github.com/apache/cordova-lib/pull/429#discussion_r60398288) [](ancestors = 60398288)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by purplecabbage <gi...@git.apache.org>.
Github user purplecabbage commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216619728
  
    Is it possible that a change to a file without affecting file size could go undetected?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60399853
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    +        var sourcePath = pathMap[targetPath];
    +        updated = updatePath(rootDir, targetPath, sourcePath, force, log) || updated;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target directory with merged files and subdirectories from source directories.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetDir Destination directory to be updated. If it does not exist, it will be
    + *     created. If it exists, newer files from source directories will be copied over, and files
    + *     missing in the source directories will be deleted.
    + * @param {string|string[]} sourceDirs Source directory or array of source directories to be
    --- End diff --
    
    This doesn't look like the valid `@param` tag According to [JSDoc guide](http://usejsdoc.org/tags-param.html) multiple types should be enclosed in parentesis


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60384873
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    --- End diff --
    
    There is a special notation for optional parameters [offered by JSDoc](http://usejsdoc.org/tags-param.html): `@param {string} [rootDir] Root directory...`. Current approach doesn't work e.g. for VSCode - it can't parse parameter type from JSDoc annotation:
    
    ![image](https://cloud.githubusercontent.com/assets/3857604/14671777/512243ae-06fc-11e6-83a0-821868da5af1.png)



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by purplecabbage <gi...@git.apache.org>.
Github user purplecabbage commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216628696
  
    Okay, thanks for clarifying. I rescind my objection. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60407237
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    +        var sourcePath = pathMap[targetPath];
    +        updated = updatePath(rootDir, targetPath, sourcePath, force, log) || updated;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target directory with merged files and subdirectories from source directories.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetDir Destination directory to be updated. If it does not exist, it will be
    + *     created. If it exists, newer files from source directories will be copied over, and files
    + *     missing in the source directories will be deleted.
    + * @param {string|string[]} sourceDirs Source directory or array of source directories to be
    + *     merged into the target. The directories are listed in order of precedence; files in
    + *     directories later in the array supersede files in directories earlier in the array
    + *     (regardless of timestamps).
    + * @param {string|string[]|null} include Optional glob string or array of glob strings that are
    + *     tested against both target and source relative paths to determine if they are include in
    + *     the merge-and-update. If null, all items are included.
    + * @param {string|string[]|null} exclude Optional glob string or array of glob strings that are
    + *     tested against both target and source relative paths to determine if they are excluded
    + *     from the merge-and-update. Exclusions override inclusions. If null, no items are excluded.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function mergeAndUpdateDir(rootDir, targetDir, sourceDirs, include, exclude, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path (or null) is required.");
    +    }
    +
    +    if (!targetDir || typeof(targetDir) !== "string") {
    +        throw new Error("A target directory path is required.");
    +    }
    +
    +    if (typeof(sourceDirs) === "string") {
    +        sourceDirs = [ sourceDirs ];
    +    } else if (!Array.isArray(sourceDirs) || sourceDirs.length === 0) {
    +        throw new Error("A source directory path or array of paths is required.");
    +    }
    +
    +    if (!include) {
    +        include = [ "**" ];
    +    } else if (typeof (include) === "string") {
    +        include = [ include ];
    +    } else if (!Array.isArray(include)) {
    +        throw new Error("Include parameter must be a glob string or array of glob strings.");
    +    }
    +
    +    if (!exclude) {
    +        exclude = [];
    +    } else if (typeof (exclude) === "string") {
    +        exclude = [ exclude ];
    +    } else if (!Array.isArray(exclude)) {
    +        throw new Error("Exclude parameter must be a glob string or array of glob strings.");
    +    }
    +
    +    // Scan the files in the target directory, if it exists.
    +    var targetMap = {};
    +    var targetFullPath = path.join(rootDir, targetDir);
    +    if (fs.existsSync(targetFullPath)) {
    +        targetMap = mapDirectory(rootDir, targetDir, include, exclude);
    +    }
    +
    +    // Scan the files in each of the source directories.
    +    var sourceMaps = [];
    +    for (var i in sourceDirs) {
    +        var sourceFullPath = path.join(rootDir, sourceDirs[i]);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source directory does not exist: " + sourceDirs[i]);
    +        }
    +        sourceMaps[i] = mapDirectory(rootDir, sourceDirs[i], include, exclude);
    +    }
    +
    +    var pathMap = mergePathMaps(targetDir, targetMap, sourceMaps);
    +
    +    var updated = false;
    +
    +    // Iterate in sorted order to ensure directories are created before files under them.
    +    Object.keys(pathMap).sort().forEach(function (subPath) {
    +        var entry = pathMap[subPath];
    +        updated = updatePathWithStats(
    +            rootDir,
    +            entry.targetPath,
    +            entry.targetStats,
    +            entry.sourcePath,
    +            entry.sourceStats,
    +            force,
    +            log) || updated;
    +    });
    +
    +    return updated;
    +}
    +
    +/**
    + * Creates a dictionary map of all files and directories under a path.
    + */
    +function mapDirectory(rootDir, subDir, include, exclude) {
    +    var dirMap = { "": { subDir: subDir, stats: fs.statSync(path.join(rootDir, subDir)) } };
    +    mapSubdirectory(rootDir, subDir, "", include, exclude, dirMap);
    +    return dirMap;
    +
    +    function mapSubdirectory(rootDir, subDir, relativeDir, include, exclude, dirMap) {
    +        var itemMapped = false;
    +        var items = fs.readdirSync(path.join(rootDir, subDir, relativeDir));
    +        for (var i in items) {
    +            var relativePath = path.join(relativeDir, items[i]);
    +
    +            // Skip any files or directories (and everything under) that match an exclude glob.
    +            if (matchGlobArray(relativePath, exclude)) {
    +                continue;
    +            }
    +
    +            // Stats obtained here (required at least to know where to recurse in directories)
    +            // are saved for later, where the modified times may also be used. This minimizes
    +            // the number of file I/O operations performed.
    +            var fullPath = path.join(rootDir, subDir, relativePath);
    +            var stats = fs.statSync(fullPath);
    +
    +            // Directories are included if either something under them is included or they
    +            // match an include glob. Files are included only if they match an include glob.
    +            if (stats.isDirectory() ?
    +                    (mapSubdirectory(rootDir, subDir, relativePath, include, exclude, dirMap) ||
    +                        matchGlobArray(relativePath, include)) :
    +                    (stats.isFile() && matchGlobArray(relativePath, include))) {
    +                dirMap[relativePath] = { subDir: subDir, stats: stats };
    +                itemMapped = true;
    +            }
    +        }
    +        return itemMapped;
    +    }
    +
    +    function matchGlobArray(path, globs) {
    +        for (var i in globs) {
    +            if (minimatch(path, globs[i])) {
    +                return true;
    +            }
    +        }
    +        return false;
    +    }
    +}
    +
    +/**
    + * Merges together multiple source maps and a target map into a single mapping from
    + * relative paths to objects with target and source paths and stats.
    + */
    +function mergePathMaps(targetDir, targetMap, sourceMaps) {
    +    // Merge multiple source maps together, along with target path info.
    +    // Entries in later source maps override those in earlier source maps.
    +    // Target stats will be filled in below for targets that exist.
    +    var pathMap = {};
    +    for (var i in sourceMaps) {
    --- End diff --
    
    Please do not use `for...in` to iterate over arrays


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60396886
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    --- End diff --
    
    According to JSDoc It is not required - could you please rephrase


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60458884
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    --- End diff --
    
    Yes, there could easily be cases this is called when no logging is desired. And logging to the console is a bad default for any Cordova code, because any logging should go through the events pipeline.
    
    ---
    In reply to: [60397076](https://github.com/apache/cordova-lib/pull/429#discussion_r60397076) [](ancestors = 60397076)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60452183
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    --- End diff --
    
    According to [http://usejsdoc.org/tags-type.html,](http://usejsdoc.org/tags-type.html,) the syntax for a nullable parameter is {?type}, which is distinct from an optional parameter.
    
    ---
    In reply to: [60448617](https://github.com/apache/cordova-lib/pull/429#discussion_r60448617) [](ancestors = 60448617,60384873)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-217283363
  
    Great! Thanks @jasongin! Merging now.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/cordova-lib/pull/429


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216624907
  
    If you're talking about a source file, then that would change the last-modified timestamp of the source file, causing it to be newer than the target file so it will be copied on the next prepare.
    
    If you save changes to a source file at precisely the wrong time _while a build is in progress_ (and the change doesn't affect the size of the file), then it's possible the change could go undetected if a hook modifies the corresponding target file to make it newer than the changed source file. But even good old 'make' has this issue.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60397531
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    --- End diff --
    
    Also, do we need to pass logging callback to every method? According to the example usage at https://github.com/apache/cordova-android/pull/295 the methods of `FileUpdater ` are called several times so it might make sense to make this class constructable ans set up logging via constructor parameter.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by omefire <gi...@git.apache.org>.
Github user omefire commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60314872
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    --- End diff --
    
    If no 'log' is specified, we should log to the console.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60399365
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    --- End diff --
    
    It is considered a bad practice to iterate through object keys using `for...in`. Please add check for `hasOwnProperty` or use `Object.keys()`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60397076
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    --- End diff --
    
    I'm not sure if we should - this would be accessed programmatically and it would be really odd if it would log something to console when it is not supposed to.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60481370
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    --- End diff --
    
    I see. Is there a reason why we need to emphasize that the `rootDir` is nullable parameter, but not optional one? From user perspective it is not very convenient to pass `null` instead of just omitting the param


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60396878
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    --- End diff --
    
    IMO `path.resolve` is more appropriate here, as it will handle correctly the case when someone has passed both `rootDir` and absolute `targetPath`. `path.join` will return incorrect path in this case.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60482215
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    --- End diff --
    
    I see your point about handling an absolute path, but I don't think I want a relative path to be always resolved to an absolute path. As documented, the caller should not specify a rootDir when using absolute paths.
    
    ---
    In reply to: [60396878](https://github.com/apache/cordova-lib/pull/429#discussion_r60396878) [](ancestors = 60396878)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216319068
  
    I updated the PR to use target creation times instead of modified-times. It works much better: it doesn't require the messy workaround on Windows and doesn't have any problems with imprecise timestamps (as on OS X).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r61665459
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,499 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +var isWindows = (process.platform === "win32");
    +var child_process = (isWindows ? require("child_process") : null);
    +
    +/**
    + * Logging callback used in the FileUpdater methods.
    + * @callback loggingCallback
    + * @param {string} message A message describing a single file update operation.
    + */
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {?string} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {?fs.Stats} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {string} targetPath Required destination file or directory to be updated. If it does
    + *     not exist, it will be created.
    + * @param {?fs.Stats} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {Object} [options] Optional additional parameters for the update.
    + * @param {string} [options.rootDir] Optional root directory (such as a project) to which target
    + *     and source path parameters are relative; may be omitted if the paths are absolute. The
    + *     rootDir is always omitted from any logged paths, to make the logs easier to read.
    + * @param {boolean} [options.all] If true, all files are copied regardless of last-modified times.
    + * @param {boolean} [options.newer] If true (and all is not specified), only files with newer
    + *     last-modified times are copied. By default, only files with different times are copied.
    + * @param {loggingCallback} [log] Optional logging callback that takes a string message
    + *     describing any file operations that are performed.
    + * @param {Object} context Context object for tracking file operations.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        sourcePath, sourceStats, targetPath, targetStats, options, log, context) {
    +    var updated = false;
    +
    +    var rootDir = (options && options.rootDir) || "";
    +    var copyAll = (options && options.all) || false;
    +    var copyNewer = (options && options.newer) || false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir  " + targetPath + " (source is a file)");
    --- End diff --
    
    Yes. Basically I wanted to enable callers to choose whether they want any logging and what level and format. I expect these functions could be useful in more than one scenario, so logging requirements may be different.
    
    Initially I am passing in events.emit.bind('verbose') as the logging callback to these functions in the updates I'm making to each platform's prepare code. But I know we may want to adjust logging levels and styles soon in the platform code, and it will be easier to do that if we don't have to update cordova-common also.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r61659743
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,499 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +var isWindows = (process.platform === "win32");
    +var child_process = (isWindows ? require("child_process") : null);
    +
    +/**
    + * Logging callback used in the FileUpdater methods.
    + * @callback loggingCallback
    + * @param {string} message A message describing a single file update operation.
    + */
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {?string} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {?fs.Stats} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {string} targetPath Required destination file or directory to be updated. If it does
    + *     not exist, it will be created.
    + * @param {?fs.Stats} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {Object} [options] Optional additional parameters for the update.
    + * @param {string} [options.rootDir] Optional root directory (such as a project) to which target
    + *     and source path parameters are relative; may be omitted if the paths are absolute. The
    + *     rootDir is always omitted from any logged paths, to make the logs easier to read.
    + * @param {boolean} [options.all] If true, all files are copied regardless of last-modified times.
    + * @param {boolean} [options.newer] If true (and all is not specified), only files with newer
    + *     last-modified times are copied. By default, only files with different times are copied.
    + * @param {loggingCallback} [log] Optional logging callback that takes a string message
    + *     describing any file operations that are performed.
    + * @param {Object} context Context object for tracking file operations.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        sourcePath, sourceStats, targetPath, targetStats, options, log, context) {
    +    var updated = false;
    +
    +    var rootDir = (options && options.rootDir) || "";
    +    var copyAll = (options && options.all) || false;
    +    var copyNewer = (options && options.newer) || false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir  " + targetPath + " (source is a file)");
    --- End diff --
    
    Rest of our code uses event emitter - is there a reason why we don't use that here?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60460847
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    +        var sourcePath = pathMap[targetPath];
    +        updated = updatePath(rootDir, targetPath, sourcePath, force, log) || updated;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target directory with merged files and subdirectories from source directories.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetDir Destination directory to be updated. If it does not exist, it will be
    + *     created. If it exists, newer files from source directories will be copied over, and files
    + *     missing in the source directories will be deleted.
    + * @param {string|string[]} sourceDirs Source directory or array of source directories to be
    + *     merged into the target. The directories are listed in order of precedence; files in
    + *     directories later in the array supersede files in directories earlier in the array
    + *     (regardless of timestamps).
    + * @param {string|string[]|null} include Optional glob string or array of glob strings that are
    + *     tested against both target and source relative paths to determine if they are include in
    + *     the merge-and-update. If null, all items are included.
    + * @param {string|string[]|null} exclude Optional glob string or array of glob strings that are
    + *     tested against both target and source relative paths to determine if they are excluded
    + *     from the merge-and-update. Exclusions override inclusions. If null, no items are excluded.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function mergeAndUpdateDir(rootDir, targetDir, sourceDirs, include, exclude, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path (or null) is required.");
    +    }
    +
    +    if (!targetDir || typeof(targetDir) !== "string") {
    +        throw new Error("A target directory path is required.");
    +    }
    +
    +    if (typeof(sourceDirs) === "string") {
    +        sourceDirs = [ sourceDirs ];
    +    } else if (!Array.isArray(sourceDirs) || sourceDirs.length === 0) {
    +        throw new Error("A source directory path or array of paths is required.");
    +    }
    +
    +    if (!include) {
    +        include = [ "**" ];
    +    } else if (typeof (include) === "string") {
    +        include = [ include ];
    +    } else if (!Array.isArray(include)) {
    +        throw new Error("Include parameter must be a glob string or array of glob strings.");
    +    }
    +
    +    if (!exclude) {
    +        exclude = [];
    +    } else if (typeof (exclude) === "string") {
    +        exclude = [ exclude ];
    +    } else if (!Array.isArray(exclude)) {
    +        throw new Error("Exclude parameter must be a glob string or array of glob strings.");
    +    }
    +
    +    // Scan the files in the target directory, if it exists.
    +    var targetMap = {};
    +    var targetFullPath = path.join(rootDir, targetDir);
    +    if (fs.existsSync(targetFullPath)) {
    +        targetMap = mapDirectory(rootDir, targetDir, include, exclude);
    +    }
    +
    +    // Scan the files in each of the source directories.
    +    var sourceMaps = [];
    +    for (var i in sourceDirs) {
    +        var sourceFullPath = path.join(rootDir, sourceDirs[i]);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source directory does not exist: " + sourceDirs[i]);
    +        }
    +        sourceMaps[i] = mapDirectory(rootDir, sourceDirs[i], include, exclude);
    +    }
    +
    +    var pathMap = mergePathMaps(targetDir, targetMap, sourceMaps);
    +
    +    var updated = false;
    +
    +    // Iterate in sorted order to ensure directories are created before files under them.
    +    Object.keys(pathMap).sort().forEach(function (subPath) {
    +        var entry = pathMap[subPath];
    +        updated = updatePathWithStats(
    +            rootDir,
    +            entry.targetPath,
    +            entry.targetStats,
    +            entry.sourcePath,
    +            entry.sourceStats,
    +            force,
    +            log) || updated;
    +    });
    +
    +    return updated;
    +}
    +
    +/**
    + * Creates a dictionary map of all files and directories under a path.
    + */
    +function mapDirectory(rootDir, subDir, include, exclude) {
    +    var dirMap = { "": { subDir: subDir, stats: fs.statSync(path.join(rootDir, subDir)) } };
    +    mapSubdirectory(rootDir, subDir, "", include, exclude, dirMap);
    +    return dirMap;
    +
    +    function mapSubdirectory(rootDir, subDir, relativeDir, include, exclude, dirMap) {
    +        var itemMapped = false;
    +        var items = fs.readdirSync(path.join(rootDir, subDir, relativeDir));
    +        for (var i in items) {
    +            var relativePath = path.join(relativeDir, items[i]);
    +
    +            // Skip any files or directories (and everything under) that match an exclude glob.
    +            if (matchGlobArray(relativePath, exclude)) {
    +                continue;
    +            }
    +
    +            // Stats obtained here (required at least to know where to recurse in directories)
    +            // are saved for later, where the modified times may also be used. This minimizes
    +            // the number of file I/O operations performed.
    +            var fullPath = path.join(rootDir, subDir, relativePath);
    +            var stats = fs.statSync(fullPath);
    +
    +            // Directories are included if either something under them is included or they
    +            // match an include glob. Files are included only if they match an include glob.
    +            if (stats.isDirectory() ?
    +                    (mapSubdirectory(rootDir, subDir, relativePath, include, exclude, dirMap) ||
    --- End diff --
    
    I wrote it as in if..else originally, but then the two lines inside the block below were duplicated. But I agree this is very hard to read as it is now, so maybe the duplication is not as bad.
    
    ---
    In reply to: [60406393](https://github.com/apache/cordova-lib/pull/429#discussion_r60406393) [](ancestors = 60406393)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60460033
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    --- End diff --
    
    Yes, I'm fixing these.
    
    ---
    In reply to: [60399365](https://github.com/apache/cordova-lib/pull/429#discussion_r60399365) [](ancestors = 60399365)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-215817627
  
    Simultaneously, lets ask libuv to make the fix for this issue.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-215255548
  
    @vladimir-kotikov I think this is ready to go after the latest update. Please let me know if you have any further comments.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216618237
  
    BTW, I didn't mention above the reason I had to abandon the approach of checking for exactly-equal modified times. The problem is the OS X filesystem doesn't support sub-second timestamps, so it would be possible for a file to be modified again in the same second while still appearing to have the same timestamp.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by codecov-io <gi...@git.apache.org>.
Github user codecov-io commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-214812288
  
    ## [Current coverage][cc-pull] is **100%**
    > Merging [#429][cc-pull] into [master][cc-base-branch] will increase coverage by **+19.90%**
    
    ```diff
    @@           master       #429   diff @@
    ========================================
      Files          69         69           
      Lines        5027       9878   +4851   
      Methods         0        841    +841   
      Branches      971       1271    +300   
    ========================================
    + Hits         4027       9878   +5851   
    + Misses        995          0    -995   
    + Partials        5          0      -5   
    ```
    
    1. 5 files in `...dova-lib/src/cordova` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F636F72646F7661) 
      - Misses `-16` 
      - Hits `+167`
    1. 2 files (not in diff) in `...lib/src/util/windows` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F7574696C2F77696E646F7773) 
      - Misses `-52` 
      - Partials `-1` 
      - Hits `+383`
    1. 3 files (not in diff) in `cordova-lib/src/util` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F7574696C) 
      - Misses `-10` 
      - Hits `+55`
    1. 7 files (not in diff) in `...lib/src/plugman/util` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F706C75676D616E2F7574696C) 
      - Misses `-19` 
      - Hits `+197`
    1. 3 files (not in diff) in `...src/plugman/registry` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F706C75676D616E2F7265676973747279) 
      - Misses `-9` 
      - Hits `+142`
    1. 7 files (not in diff) in `...rc/plugman/platforms` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F706C75676D616E2F706C6174666F726D73) 
      - Misses `-84` 
      - Hits `+608`
    1. 12 files (not in diff) in `...dova-lib/src/plugman` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F706C75676D616E) 
      - Misses `-261` 
      - Hits `+1202`
    1. 2 files (not in diff) in `...va-lib/src/platforms` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F706C6174666F726D73) 
      - Misses `-12` 
      - Partials `-2` 
      - Hits `+243`
    1. 3 files (not in diff) in `...ordova-lib/src/hooks` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F686F6F6B73) 
      - Misses `-48` 
      - Hits `+208`
    1. 2 files (not in diff) in `...etadata/parserhelper` were modified. [more](https://codecov.io/gh/apache/cordova-lib/commit/3abe8b389628cc0256f16fa6d66da7a460485925/changes?src=pr#636F72646F76612D6C69622F7372632F636F72646F76612F6D657461646174612F70617273657268656C706572) 
      - Hits `+64`
    
    [![Sunburst](https://codecov.io/gh/apache/cordova-lib/pull/429/graphs/sunburst.svg?size=660&src=pr)][cc-pull]
    
    
    > Powered by [Codecov](https://codecov.io?src=pr). Last updated by 3abe8b3
    [cc-base-branch]: https://codecov.io/gh/apache/cordova-lib/branch/master?src=pr
    [cc-pull]: https://codecov.io/gh/apache/cordova-lib/pull/429?src=pr


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-215260110
  
    LGTM


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60406393
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    +        var sourcePath = pathMap[targetPath];
    +        updated = updatePath(rootDir, targetPath, sourcePath, force, log) || updated;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target directory with merged files and subdirectories from source directories.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetDir Destination directory to be updated. If it does not exist, it will be
    + *     created. If it exists, newer files from source directories will be copied over, and files
    + *     missing in the source directories will be deleted.
    + * @param {string|string[]} sourceDirs Source directory or array of source directories to be
    + *     merged into the target. The directories are listed in order of precedence; files in
    + *     directories later in the array supersede files in directories earlier in the array
    + *     (regardless of timestamps).
    + * @param {string|string[]|null} include Optional glob string or array of glob strings that are
    + *     tested against both target and source relative paths to determine if they are include in
    + *     the merge-and-update. If null, all items are included.
    + * @param {string|string[]|null} exclude Optional glob string or array of glob strings that are
    + *     tested against both target and source relative paths to determine if they are excluded
    + *     from the merge-and-update. Exclusions override inclusions. If null, no items are excluded.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function mergeAndUpdateDir(rootDir, targetDir, sourceDirs, include, exclude, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path (or null) is required.");
    +    }
    +
    +    if (!targetDir || typeof(targetDir) !== "string") {
    +        throw new Error("A target directory path is required.");
    +    }
    +
    +    if (typeof(sourceDirs) === "string") {
    +        sourceDirs = [ sourceDirs ];
    +    } else if (!Array.isArray(sourceDirs) || sourceDirs.length === 0) {
    +        throw new Error("A source directory path or array of paths is required.");
    +    }
    +
    +    if (!include) {
    +        include = [ "**" ];
    +    } else if (typeof (include) === "string") {
    +        include = [ include ];
    +    } else if (!Array.isArray(include)) {
    +        throw new Error("Include parameter must be a glob string or array of glob strings.");
    +    }
    +
    +    if (!exclude) {
    +        exclude = [];
    +    } else if (typeof (exclude) === "string") {
    +        exclude = [ exclude ];
    +    } else if (!Array.isArray(exclude)) {
    +        throw new Error("Exclude parameter must be a glob string or array of glob strings.");
    +    }
    +
    +    // Scan the files in the target directory, if it exists.
    +    var targetMap = {};
    +    var targetFullPath = path.join(rootDir, targetDir);
    +    if (fs.existsSync(targetFullPath)) {
    +        targetMap = mapDirectory(rootDir, targetDir, include, exclude);
    +    }
    +
    +    // Scan the files in each of the source directories.
    +    var sourceMaps = [];
    +    for (var i in sourceDirs) {
    +        var sourceFullPath = path.join(rootDir, sourceDirs[i]);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source directory does not exist: " + sourceDirs[i]);
    +        }
    +        sourceMaps[i] = mapDirectory(rootDir, sourceDirs[i], include, exclude);
    +    }
    +
    +    var pathMap = mergePathMaps(targetDir, targetMap, sourceMaps);
    +
    +    var updated = false;
    +
    +    // Iterate in sorted order to ensure directories are created before files under them.
    +    Object.keys(pathMap).sort().forEach(function (subPath) {
    +        var entry = pathMap[subPath];
    +        updated = updatePathWithStats(
    +            rootDir,
    +            entry.targetPath,
    +            entry.targetStats,
    +            entry.sourcePath,
    +            entry.sourceStats,
    +            force,
    +            log) || updated;
    +    });
    +
    +    return updated;
    +}
    +
    +/**
    + * Creates a dictionary map of all files and directories under a path.
    + */
    +function mapDirectory(rootDir, subDir, include, exclude) {
    +    var dirMap = { "": { subDir: subDir, stats: fs.statSync(path.join(rootDir, subDir)) } };
    +    mapSubdirectory(rootDir, subDir, "", include, exclude, dirMap);
    +    return dirMap;
    +
    +    function mapSubdirectory(rootDir, subDir, relativeDir, include, exclude, dirMap) {
    +        var itemMapped = false;
    +        var items = fs.readdirSync(path.join(rootDir, subDir, relativeDir));
    +        for (var i in items) {
    +            var relativePath = path.join(relativeDir, items[i]);
    +
    +            // Skip any files or directories (and everything under) that match an exclude glob.
    +            if (matchGlobArray(relativePath, exclude)) {
    +                continue;
    +            }
    +
    +            // Stats obtained here (required at least to know where to recurse in directories)
    +            // are saved for later, where the modified times may also be used. This minimizes
    +            // the number of file I/O operations performed.
    +            var fullPath = path.join(rootDir, subDir, relativePath);
    +            var stats = fs.statSync(fullPath);
    +
    +            // Directories are included if either something under them is included or they
    +            // match an include glob. Files are included only if they match an include glob.
    +            if (stats.isDirectory() ?
    +                    (mapSubdirectory(rootDir, subDir, relativePath, include, exclude, dirMap) ||
    --- End diff --
    
    Please split this ternary into `if...else` to make it readable


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60459281
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    --- End diff --
    
    Not a bad suggestion, but I'm leaning toward keeping it this way. I can easily imagine multiple calls to FileUpdater methods using different logging. (And I'll refactor the code in the other PR so the logging callback code isn't duplicated.)
    
    ---
    In reply to: [60397531](https://github.com/apache/cordova-lib/pull/429#discussion_r60397531) [](ancestors = 60397531)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60409457
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    --- End diff --
    
    nit: parenthesis are not needed in `typeof` expression


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60398288
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    --- End diff --
    
    The `if` block below is executed when `targetStats` is defined so probably id would be better to place it right after [line 72](https://github.com/apache/cordova-lib/pull/429/files#diff-8b56245d31f26df256f8361f42457c84R72)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60398700
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    --- End diff --
    
    it would not throw if pathMap is an array (`typeof [] === 'object'` is truthy)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216617718
  
    @purplecabbage If a hook fails because it expected a file would be refreshed but it wasn't, then I'd expect the hook to print an error (or stack trace), so that should be easy to find. Is there another scenario you're concerned about?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-215911697
  
    @nikhilkh As we discussed, this workaround was the best I could find. The command script approach has similar performance to just copying files via the node fs APIs, and it correctly copies the file times, enabling incremental builds to work correctly. My biggest remaining concern is any errors during the copy will not pinpoint a specific file, but that should be an extremely rare issue.
    
    I also tried using a generated PowerShell script to just update the file times after copying in nodejs, but that was too slow, both in PS startup cost and per-file cost.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-217281613
  
    As discussed, I tested this with symbolic links both at the root and in a subdirectory. It works fine without any special symlink handling in the code.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by vladimir-kotikov <gi...@git.apache.org>.
Github user vladimir-kotikov commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60396904
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    --- End diff --
    
    A few comments on the method signature:
    
    1. I'd personally prefer this method signature to follow standart semantics for copying files: source arg goes first then destination (e.g. `cp --help`):
    ```
      Usage: cp [OPTION]... [-T] SOURCE DEST
      or:  cp [OPTION]... SOURCE... DIRECTORY
    ```
      or `copy /?`
    ```
      COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
         [+ source [/A | /B] [+ ...]] [destination [/A | /B]]
    ```
    2. It is common practice to put optional arguments after required ones, so probably `rootDir` is better to be placed after `sourcePath` and `targetPath`
    3. I'd pass all optional arguments (except `sourcePath`) as an options object (like `updatePath('./foo', './bar', null, true)` vs. `updatePath('./foo', './bar', {force:  true})`)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60448617
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    --- End diff --
    
    I was aware of that syntax (and used it for the [log] parameter here). But my interpretation was that it was for parameters that could be omitted by the caller, not merely parameters that could be null. The JSDoc documentation isn't clear about that, but everywhere else I've seen the brackets used was where the parameter could be omitted.
    
    ---
    In reply to: [60384873](https://github.com/apache/cordova-lib/pull/429#discussion_r60384873) [](ancestors = 60384873)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216585668
  
    Well, after further testing I found creation dates don't work reliably due to some strange behavior on Windows: http://serverfault.com/questions/92757/incorrect-file-creation-date-in-windows-xp-vista
    
    So I'm still working on finding the best solution here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-215780851
  
    I need to add a workaround for the nodejs bug in setting file timestamps on Windows: https://github.com/nodejs/node/issues/2069


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60450430
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    --- End diff --
    
    Yes, I'll rearrange the parameters.
    
    ---
    In reply to: [60396904](https://github.com/apache/cordova-lib/pull/429#discussion_r60396904) [](ancestors = 60396904)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/429#discussion_r60449617
  
    --- Diff: cordova-common/src/FileUpdater.js ---
    @@ -0,0 +1,389 @@
    +/**
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +    http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +"use strict";
    +
    +var fs = require("fs");
    +var path = require("path");
    +var shell = require("shelljs");
    +var minimatch = require("minimatch");
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.) Stats for target and source items must be passed in. This is an internal
    + * helper function used by other methods in this module.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {fs.Stats|null} targetStats An instance of fs.Stats for the target path, or null if
    + *     the target does not exist.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {fs.Stats|null} sourceStats An instance of fs.Stats for the source path, or null if
    + *     the source does not exist.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log) {
    +    log = log || function(message) { };
    +    var updated = false;
    +
    +    var targetFullPath = path.join(rootDir || "", targetPath);
    +
    +    if (sourceStats) {
    +        var sourceFullPath = path.join(rootDir || "", sourcePath);
    +
    +        if (targetStats) {
    +            // The target exists. But if the directory status doesn't match the source, delete it.
    +            if (targetStats.isDirectory() && !sourceStats.isDirectory()) {
    +                log("rmdir " + targetPath + " (source is a file)");
    +                shell.rm("-rf", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            } else if (!targetStats.isDirectory() && sourceStats.isDirectory()) {
    +                log("delete " + targetPath + " (source is a directory)");
    +                shell.rm("-f", targetFullPath);
    +                targetStats = null;
    +                updated = true;
    +            }
    +        }
    +
    +        if (!targetStats) {
    +            if (sourceStats.isDirectory()) {
    +                // The target directory does not exist, so it should be created.
    +                log("mkdir " + targetPath);
    +                shell.mkdir("-p", targetFullPath);
    +                updated = true;
    +            } else if (sourceStats.isFile()) {
    +                // The target file does not exist, so it should be copied from the source.
    +                log("copy " + sourcePath + " " + targetPath +
    +                    (!force ? " (new file)" : ""));
    +                shell.cp("-f", sourceFullPath, targetFullPath);
    +                updated = true;
    +            }
    +        } else if (sourceStats.isFile() && targetStats.isFile() &&
    +                (force || sourceStats.mtime > targetStats.mtime)) {
    +            // When the source and target paths both exist and are files, update
    +            // the file if the source is newer or if doing a forced update.
    +            log("copy " + sourcePath + " " + targetPath +
    +                (!force ? " (updated file)" : ""));
    +            shell.cp("-f", sourceFullPath, targetFullPath);
    +            updated = true;
    +        }
    +    } else if (targetStats) {
    +        // The target exists but the source is null, so the target should be deleted.
    +        if (targetStats.isDirectory()) {
    +            log("rmdir " + targetPath + " (no source)");
    +            shell.rm("-rf", targetFullPath);
    +        } else {
    +            log("delete " + targetPath + " (no source)");
    +            shell.rm("-f", targetFullPath);
    +        }
    +        updated = true;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target file or directory with a source file or directory. (Directory updates are
    + * not recursive.)
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetPath Destination file or directory to be updated. If it does not exist,
    + *     it will be created.
    + * @param {string|null} sourcePath Source file or directory to be used to update the
    + *     destination. If the source is null, then the destination is deleted if it exists.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePath(rootDir, targetPath, sourcePath, force, log) {
    +    rootDir = rootDir || "";
    +    if (typeof(rootDir) !== "string") {
    +        throw new Error("A root directory path is required.");
    +    }
    +
    +    if (!targetPath || typeof(targetPath) !== "string") {
    +        throw new Error("A target path is required.");
    +    }
    +
    +    if (sourcePath && typeof(sourcePath) !== "string") {
    +        throw new Error("A source path (or null) is required.");
    +    }
    +
    +    log = log || function(message) { };
    +
    +    var targetFullPath = path.join(rootDir, targetPath);
    +    var targetStats = fs.existsSync(targetFullPath) ? fs.statSync(targetFullPath) : null;
    +    var sourceStats = null;
    +
    +    if (sourcePath) {
    +        // A non-null source path was specified. It should exist.
    +        var sourceFullPath = path.join(rootDir, sourcePath);
    +        if (!fs.existsSync(sourceFullPath)) {
    +            throw new Error("Source path does not exist: " + sourcePath);
    +        }
    +
    +        sourceStats = fs.statSync(sourceFullPath);
    +
    +        // Create the target's parent directory if it doesn't exist.
    +        var parentDir = path.dirname(targetFullPath);
    +        if (!fs.existsSync(parentDir)) {
    +            shell.mkdir("-p", parentDir);
    +        }
    +    }
    +
    +    return updatePathWithStats(
    +        rootDir, targetPath, targetStats, sourcePath, sourceStats, force, log);
    +}
    +
    +/**
    + * Updates files and directories based on a mapping from target paths to source paths. Targets
    + * with null sources in the map are deleted.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {object} pathMap A dictionary mapping from target paths to source paths.
    + * @param {boolean} force If target and source are both files, and the force flag is not
    + *     set, then the file will not be copied unless the source is newer than the target.
    + * @param {function} [log] Optional logging callback that takes a string message describing any
    + *     file operations that are performed.
    + * @return {boolean} true if any changes were made, or false if the force flag is not set
    + *     and everything was up to date
    + */
    +function updatePaths(rootDir, pathMap, force, log) {
    +    if (!pathMap || typeof(pathMap) !== "object") {
    +        throw new Error("An object mapping from target paths to source paths is required.");
    +    }
    +
    +    var updated = false;
    +
    +    for (var targetPath in pathMap) {
    +        var sourcePath = pathMap[targetPath];
    +        updated = updatePath(rootDir, targetPath, sourcePath, force, log) || updated;
    +    }
    +
    +    return updated;
    +}
    +
    +/**
    + * Updates a target directory with merged files and subdirectories from source directories.
    + *
    + * @param {string|null} rootDir Root directory (such as a project) to which target and source
    + *     path parameters are relative, or null if the paths are absolute. The rootDir is omitted
    + *     from any logged paths, to make the logs easier to read.
    + * @param {string} targetDir Destination directory to be updated. If it does not exist, it will be
    + *     created. If it exists, newer files from source directories will be copied over, and files
    + *     missing in the source directories will be deleted.
    + * @param {string|string[]} sourceDirs Source directory or array of source directories to be
    --- End diff --
    
    Most uses of JSDoc that I've seen omit those parentheses. And VS Code correctly parses it without parentheses.
    
    ---
    In reply to: [60399853](https://github.com/apache/cordova-lib/pull/429#discussion_r60399853) [](ancestors = 60399853)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


[GitHub] cordova-lib pull request: CB-11117: Add FileUpdater module to cord...

Posted by jasongin <gi...@git.apache.org>.
Github user jasongin commented on the pull request:

    https://github.com/apache/cordova-lib/pull/429#issuecomment-216611405
  
    All right, I think this is the best we can do. The latest logic copies the file if the source's last-modified time is >= the target's last-modified time, or if the file sizes are different. At the time the file gets copied, the target's last-modified time gets set to the current time, so it is greater than the source's time until the source gets modified again.
    
    This should work fine with hooks, because a hook that modifies a file is likely to change the size, or if it doesn't change the size then the hook is likely to handle the file not being refreshed. One could contrive an artificial scenario in which the right thing doesn't happen, but in practice I don't think there would be any issues. And still there will be the 'clean' functionality to fall back on.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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