Пример #1
0
 var filterIndexFile = function (fileInfo) {
     if (fileInfo.fullPath.indexOf(containingFolder) === 0) {
         if (getFilenameWithoutExtension(fileInfo.name) === "index") {
             if (hasOwnServerForLiveDevelopment) {
                 if ((FileUtils.isServerHtmlFileExt(fileInfo.name)) ||
                         (FileUtils.isStaticHtmlFileExt(fileInfo.name))) {
                     return true;
                 }
             } else if (FileUtils.isStaticHtmlFileExt(fileInfo.name)) {
                 return true;
             }
         } else {
             return false;
         }
     }
 };
Пример #2
0
    UserServer.prototype.canServe = function (localPath) {
        // UserServer can only function when the project specifies a base URL
        if (!this._baseUrl) {
            return false;
        }

        // If we can't transform the local path to a project relative path,
        // the path cannot be served
        if (localPath === this._pathResolver(localPath)) {
            return false;
        }

        return FileUtils.isStaticHtmlFileExt(localPath) ||
            FileUtils.isServerHtmlFileExt(localPath);
    };
Пример #3
0
 function _prepareServer(doc) {
     var deferred = new $.Deferred();
     
     _server = LiveDevServerManager.getServer(doc.file.fullPath);
     
     if (!exports.config.experimental && !_server) {
         if (FileUtils.isServerHtmlFileExt(doc.extension)) {
             PreferencesDialogs.showProjectPreferencesDialog("", Strings.LIVE_DEV_NEED_BASEURL_MESSAGE)
                 .done(function (id) {
                     if (id === Dialogs.DIALOG_BTN_OK && ProjectManager.getBaseUrl()) {
                         // If base url is specifed, then re-invoke _prepareServer() to continue
                         _prepareServer(doc).then(deferred.resolve, deferred.reject);
                     } else {
                         deferred.reject();
                     }
                 });
         } else if (!FileUtils.isStaticHtmlFileExt(doc.extension)) {
             _showWrongDocError();
             deferred.reject();
         } else {
             // fall-back to file://
             deferred.resolve();
         }
     } else {
         var readyPromise = _server.readyToServe();
         if (!readyPromise) {
             _showLiveDevServerNotReadyError();
             deferred.reject();
         } else {
             readyPromise.then(deferred.resolve, function () {
                 _showLiveDevServerNotReadyError();
                 deferred.reject();
             });
         }
     }
     
     return deferred.promise();
 }
Пример #4
0
    /** Open the Connection and go live */
    function open() {
        var result = new $.Deferred(),
            promise = result.promise();
        var doc = _getCurrentDocument();
        var browserStarted = false;
        var retryCount = 0;

        function showWrongDocError() {
            Dialogs.showModalDialog(
                Dialogs.DIALOG_ID_ERROR,
                Strings.LIVE_DEVELOPMENT_ERROR_TITLE,
                Strings.LIVE_DEV_NEED_HTML_MESSAGE
            );
            result.reject();
        }

        function showNeedBaseUrlError() {
            PreferencesDialogs.showProjectPreferencesDialog("", Strings.LIVE_DEV_NEED_BASEURL_MESSAGE)
                .done(function (id) {
                    if (id === Dialogs.DIALOG_BTN_OK && ProjectManager.getBaseUrl()) {
                        // If base url is specifed, then re-invoke open() to continue
                        open();
                        result.resolve();
                    } else {
                        result.reject();
                    }
                })
                .fail(function () {
                    result.reject();
                });
        }

        if (!doc || !doc.root) {
            showWrongDocError();

        } else {
            if (!exports.config.experimental) {
                if (FileUtils.isServerHtmlFileExt(doc.extension)) {
                    if (!ProjectManager.getBaseUrl()) {
                        showNeedBaseUrlError();
                        return promise;
                    }
                } else if (!FileUtils.isStaticHtmlFileExt(doc.extension)) {
                    showWrongDocError();
                    return promise;
                }
            }

            var url = doc.root.url;

            _setStatus(STATUS_CONNECTING);
            Inspector.connectToURL(url).then(result.resolve, function onConnectFail(err) {
                if (err === "CANCEL") {
                    result.reject(err);
                    return;
                }
                if (retryCount > 6) {
                    _setStatus(STATUS_ERROR);
                    Dialogs.showModalDialog(
                        Dialogs.DIALOG_ID_LIVE_DEVELOPMENT,
                        Strings.LIVE_DEVELOPMENT_RELAUNCH_TITLE,
                        Strings.LIVE_DEVELOPMENT_ERROR_MESSAGE
                    ).done(function (id) {
                        if (id === Dialogs.DIALOG_BTN_OK) {
                            // User has chosen to reload Chrome, quit the running instance
                            _setStatus(STATUS_INACTIVE);
                            NativeApp.closeLiveBrowser()
                                .done(function () {
                                    browserStarted = false;
                                    window.setTimeout(function () {
                                        open().then(result.resolve, result.reject);
                                    });
                                })
                                .fail(function (err) {
                                    // Report error?
                                    _setStatus(STATUS_ERROR);
                                    browserStarted = false;
                                    result.reject("CLOSE_LIVE_BROWSER");
                                });
                        } else {
                            result.reject("CANCEL");
                        }
                    });
                    return;
                }
                retryCount++;

                if (!browserStarted && exports.status !== STATUS_ERROR) {
                    url = launcherUrl + '?' + encodeURIComponent(url);

                    // If err === FileError.ERR_NOT_FOUND, it means a remote debugger connection
                    // is available, but the requested URL is not loaded in the browser. In that
                    // case we want to launch the live browser (to open the url in a new tab)
                    // without using the --remote-debugging-port flag. This works around issues
                    // on Windows where Chrome can't be opened more than once with the
                    // --remote-debugging-port flag set.
                    NativeApp.openLiveBrowser(
                        url,
                        err !== FileError.ERR_NOT_FOUND
                    )
                        .done(function () {
                            browserStarted = true;
                        })
                        .fail(function (err) {
                            var message;

                            _setStatus(STATUS_ERROR);
                            if (err === FileError.NOT_FOUND_ERR) {
                                message = Strings.ERROR_CANT_FIND_CHROME;
                            } else {
                                message = StringUtils.format(Strings.ERROR_LAUNCHING_BROWSER, err);
                            }
                            
                            // Append a message to direct users to the troubleshooting page.
                            if (message) {
                                message += " " + StringUtils.format(Strings.LIVE_DEVELOPMENT_TROUBLESHOOTING, brackets.config.troubleshoot_url);
                            }

                            Dialogs.showModalDialog(
                                Dialogs.DIALOG_ID_ERROR,
                                Strings.ERROR_LAUNCHING_BROWSER_TITLE,
                                message
                            );

                            result.reject("OPEN_LIVE_BROWSER");
                        });
                }
                    
                if (exports.status !== STATUS_ERROR) {
                    window.setTimeout(function retryConnect() {
                        Inspector.connectToURL(url).then(result.resolve, onConnectFail);
                    }, 500);
                }
            });
        }

        return promise;
    }
Пример #5
0
 function _isHtmlFileExt(ext) {
     return (FileUtils.isStaticHtmlFileExt(ext) ||
             (ProjectManager.getBaseUrl() && FileUtils.isServerHtmlFileExt(ext)));
 }
Пример #6
0
    /**
     * @private
     * Determine an index file that can be used to start Live Development.
     * This function will inspect all files in a project to find the closest index file
     * available for currently opened document. We are searching for these files:
     *  - index.html
     *  - index.htm
     * 
     * If the project is configured with a custom base url for live developmment, then
     * the list of possible index files is extended to contain these index files too:
     *  - index.php
     *  - index.php3
     *  - index.php4
     *  - index.php5
     *  - index.phtm
     *  - index.phtml
     *  - index.cfm
     *  - index.cfml
     *  - index.asp
     *  - index.aspx
     *  - index.jsp
     *  - index.jspx
     *  - index.shm
     *  - index.shml
     * 
     * If a file was found, the promise will be resolved with the full path to this file. If no file
     * was found in the whole project tree, the promise will be resolved with null.
     * 
     * @return {jQuery.Promise} A promise that is resolved with a full path
     * to a file if one could been determined, or null if there was no suitable index
     * file.
     */
    function _getInitialDocFromCurrent() {
        var doc = _getCurrentDocument(),
            refPath,
            i;

        // TODO: FileUtils.getParentFolder()
        function getParentFolder(path) {
            return path.substring(0, path.lastIndexOf('/', path.length - 2) + 1);
        }

        function getFilenameWithoutExtension(filename) {
            var index = filename.lastIndexOf(".");
            return index === -1 ? filename : filename.slice(0, index);
        }

        // Is the currently opened document already a file we can use for Live Development?
        if (doc) {
            refPath = doc.file.fullPath;
            if (FileUtils.isStaticHtmlFileExt(refPath) || FileUtils.isServerHtmlFileExt(refPath)) {
                return new $.Deferred().resolve(doc);
            }
        }

        var result = new $.Deferred();

        var baseUrl = ProjectManager.getBaseUrl(),
            hasOwnServerForLiveDevelopment = (baseUrl && baseUrl.length);

        FileIndexManager.getFileInfoList("all").done(function (allFiles) {
            if (refPath) {
                var projectRoot = ProjectManager.getProjectRoot().fullPath,
                    containingFolder = FileUtils.getDirectoryPath(refPath),
                    indexFileFound = false,
                    stillInProjectTree = true;

                var filteredFiltered = allFiles.filter(function (item) {
                    var parent = getParentFolder(item.fullPath);
                    
                    return (containingFolder.indexOf(parent) === 0);
                });
                
                var filterIndexFile = function (fileInfo) {
                    if (fileInfo.fullPath.indexOf(containingFolder) === 0) {
                        if (getFilenameWithoutExtension(fileInfo.name) === "index") {
                            if (hasOwnServerForLiveDevelopment) {
                                if ((FileUtils.isServerHtmlFileExt(fileInfo.name)) ||
                                        (FileUtils.isStaticHtmlFileExt(fileInfo.name))) {
                                    return true;
                                }
                            } else if (FileUtils.isStaticHtmlFileExt(fileInfo.name)) {
                                return true;
                            }
                        } else {
                            return false;
                        }
                    }
                };

                while (!indexFileFound && stillInProjectTree) {
                    i = CollectionUtils.indexOf(filteredFiltered, filterIndexFile);

                    // We found no good match
                    if (i === -1) {
                        // traverse the directory tree up one level
                        containingFolder = getParentFolder(containingFolder);
                        // Are we still inside the project?
                        if (containingFolder.indexOf(projectRoot) === -1) {
                            stillInProjectTree = false;
                        }
                    } else {
                        indexFileFound = true;
                    }
                }

                if (i !== -1) {
                    DocumentManager.getDocumentForPath(filteredFiltered[i].fullPath).then(result.resolve, result.resolve);
                    return;
                }
            }

            result.resolve(null);
        });

        return result.promise();
    }
Пример #7
0
 FileServer.prototype.canServe = function (localPath) {
     // FileServer requires that the base URL is undefined and static HTML files
     return (!this._baseUrl && FileUtils.isStaticHtmlFileExt(localPath));
 };
Пример #8
0
    /** Open the Connection and go live */
    function open() {
        var result = new $.Deferred(),
            promise = result.promise();
        var doc = _getCurrentDocument();
        var browserStarted = false;
        var retryCount = 0;

        function showWrongDocError() {
            Dialogs.showModalDialog(
                Dialogs.DIALOG_ID_ERROR,
                Strings.LIVE_DEVELOPMENT_ERROR_TITLE,
                Strings.LIVE_DEV_NEED_HTML_MESSAGE
            );
            result.reject();
        }

        function showNeedBaseUrlError() {
            PreferencesDialogs.showProjectPreferencesDialog("", Strings.LIVE_DEV_NEED_BASEURL_MESSAGE)
                .done(function (id) {
                    if (id === Dialogs.DIALOG_BTN_OK && ProjectManager.getBaseUrl()) {
                        // If base url is specifed, then re-invoke open() to continue
                        open();
                        result.resolve();
                    } else {
                        result.reject();
                    }
                })
                .fail(function () {
                    result.reject();
                });
        }

        function showLiveDevServerNotReadyError() {
            Dialogs.showModalDialog(
                Dialogs.DIALOG_ID_ERROR,
                Strings.LIVE_DEVELOPMENT_ERROR_TITLE,
                Strings.LIVE_DEV_SERVER_NOT_READY_MESSAGE
            );
            result.reject();
        }
        
        // helper function that actually does the launch once we are sure we have
        // a doc and the server for that doc is up and running.
        function doLaunchAfterServerReady() {
            var targetUrl = doc.root.url;
            var interstitialUrl = launcherUrl + "?" + encodeURIComponent(targetUrl);

            _setStatus(STATUS_CONNECTING);
            Inspector.connectToURL(interstitialUrl).done(result.resolve).fail(function onConnectFail(err) {
                if (err === "CANCEL") {
                    result.reject(err);
                    return;
                }
                if (retryCount > 6) {
                    _setStatus(STATUS_ERROR);
                    Dialogs.showModalDialog(
                        Dialogs.DIALOG_ID_LIVE_DEVELOPMENT,
                        Strings.LIVE_DEVELOPMENT_RELAUNCH_TITLE,
                        Strings.LIVE_DEVELOPMENT_ERROR_MESSAGE
                    ).done(function (id) {
                        if (id === Dialogs.DIALOG_BTN_OK) {
                            // User has chosen to reload Chrome, quit the running instance
                            _setStatus(STATUS_INACTIVE);
                            NativeApp.closeLiveBrowser()
                                .done(function () {
                                    browserStarted = false;
                                    window.setTimeout(function () {
                                        open().done(result.resolve).fail(result.reject);
                                    });
                                })
                                .fail(function (err) {
                                    // Report error?
                                    _setStatus(STATUS_ERROR);
                                    browserStarted = false;
                                    result.reject("CLOSE_LIVE_BROWSER");
                                });
                        } else {
                            result.reject("CANCEL");
                        }
                    });
                    return;
                }
                retryCount++;

                if (!browserStarted && exports.status !== STATUS_ERROR) {
                    NativeApp.openLiveBrowser(
                        interstitialUrl,
                        true        // enable remote debugging
                    )
                        .done(function () {
                            browserStarted = true;
                        })
                        .fail(function (err) {
                            var message;

                            _setStatus(STATUS_ERROR);
                            if (err === NativeFileError.NOT_FOUND_ERR) {
                                message = Strings.ERROR_CANT_FIND_CHROME;
                            } else {
                                message = StringUtils.format(Strings.ERROR_LAUNCHING_BROWSER, err);
                            }
                            
                            // Append a message to direct users to the troubleshooting page.
                            if (message) {
                                message += " " + StringUtils.format(Strings.LIVE_DEVELOPMENT_TROUBLESHOOTING, brackets.config.troubleshoot_url);
                            }

                            Dialogs.showModalDialog(
                                Dialogs.DIALOG_ID_ERROR,
                                Strings.ERROR_LAUNCHING_BROWSER_TITLE,
                                message
                            );

                            result.reject("OPEN_LIVE_BROWSER");
                        });
                }
                    
                if (exports.status !== STATUS_ERROR) {
                    window.setTimeout(function retryConnect() {
                        Inspector.connectToURL(interstitialUrl).done(result.resolve).fail(onConnectFail);
                    }, 500);
                }
            });
        }
        
        if (!doc || !doc.root) {
            showWrongDocError();

        } else {
            _serverProvider = LiveDevServerManager.getProvider(doc.file.fullPath);
            if (!exports.config.experimental && !_serverProvider) {
                if (FileUtils.isServerHtmlFileExt(doc.extension)) {
                    showNeedBaseUrlError();
                } else if (!FileUtils.isStaticHtmlFileExt(doc.extension)) {
                    showWrongDocError();
                } else {
                    doLaunchAfterServerReady();   // fall-back to file://
                }
            } else {
                var readyPromise = _serverProvider.readyToServe();
                if (!readyPromise) {
                    showLiveDevServerNotReadyError();
                } else {
                    readyPromise.then(
                        doLaunchAfterServerReady,
                        showLiveDevServerNotReadyError
                    );
                }
            }
        }

        return promise;
    }