Exemplo n.º 1
0
        /// <summary>
        /// Removes the specified script.
        /// </summary>
        /// <param name="scriptName">Name of script to remove.</param>
        public void RemoveScript(String scriptName)
        {
            // Cannot create an autoscript from the client.
            if (isAutoScript(scriptName))
            {
                throw new StackHashException("Cannot remove system scripts", StackHashServiceErrorCode.CannotRemoveSystemScripts);
            }

            String fileName = getScriptFileName(scriptName);

            // Load it to see if it is read only.
            if (File.Exists(fileName))
            {
                StackHashScriptSettings script = StackHashScriptSettings.Load(fileName);

                if (!script.IsReadOnly)
                {
                    File.Delete(fileName);
                }
                else
                {
                    throw new StackHashException("Cannot remove Read Only script", StackHashServiceErrorCode.CannotRemoveSystemScripts);
                }
            }
        }
Exemplo n.º 2
0
        public void UpdateScriptFile()
        {
            bool saveAutoScript = false;
            bool fileExists     = File.Exists(m_ScriptFileName);

            if (fileExists)
            {
                // Load in the script and check the version number. If there is an error during load
                // then just create a new copy of the file.
                try
                {
                    StackHashScriptSettings thisScript = StackHashScriptSettings.Load(m_ScriptFileName);
                    saveAutoScript = !IsScriptCurrent(thisScript);
                }
                catch (System.Exception ex)
                {
                    String message = String.Format(CultureInfo.InvariantCulture, "Failed to load script {0} - Reconstructing", ScriptName);
                    DiagnosticsHelper.LogException(DiagSeverity.Warning, message, ex);
                    saveAutoScript = true;
                }
            }
            else
            {
                saveAutoScript = true;
            }

            FileAttributes currentAttributes;

            if (saveAutoScript)
            {
                if (fileExists)
                {
                    currentAttributes = File.GetAttributes(m_ScriptFileName);

                    // Turn off the readonly permission so the file can be updated.
                    if ((currentAttributes & FileAttributes.ReadOnly) != 0)
                    {
                        // Clear the read only flag.
                        File.SetAttributes(m_ScriptFileName, currentAttributes & ~FileAttributes.ReadOnly);
                    }
                }
                StackHashScriptSettings autoScript = GenerateScript();
                autoScript.Save(m_ScriptFileName);
            }

            // Make sure the file is marked read only so the client can't delete it.
            currentAttributes = File.GetAttributes(m_ScriptFileName);
            if ((currentAttributes & FileAttributes.ReadOnly) == 0)
            {
                // Set the read only flag.
                File.SetAttributes(m_ScriptFileName, currentAttributes | FileAttributes.ReadOnly);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets all script contexts.
        /// </summary>
        /// <returns>Full list of all the script settings.</returns>
        private StackHashScriptCollection GetAllScripts()
        {
            StackHashScriptCollection allScripts = new StackHashScriptCollection();

            String [] allScriptFiles = Directory.GetFiles(m_ScriptFolder, s_ScriptWildcard);

            foreach (String fileName in allScriptFiles)
            {
                StackHashScriptSettings thisScript = StackHashScriptSettings.Load(fileName);

                allScripts.Add(thisScript);
            }

            return(allScripts);
        }
Exemplo n.º 4
0
        private void scriptSaveLoadNCommands(int numCommands, bool addComment)
        {
            StackHashScript script = new StackHashScript();

            for (int i = 0; i < numCommands; i++)
            {
                String command = "command" + i.ToString();
                String comment = null;
                if (addComment)
                {
                    comment = "comment" + i.ToString();
                }
                script.Add(new StackHashScriptLine(command, comment));
            }
            String testScriptName = m_TempPath + "\\scriptsettings.wds";
            StackHashScriptSettings scriptSettings = new StackHashScriptSettings(testScriptName, script);

            scriptSettings.Name             = "TestName";
            scriptSettings.CreationDate     = DateTime.Now.ToUniversalTime();
            scriptSettings.LastModifiedDate = DateTime.Now.ToUniversalTime();
            scriptSettings.Owner            = StackHashScriptOwner.System;
            scriptSettings.RunAutomatically = true;
            scriptSettings.Version          = 2;
            scriptSettings.Save(testScriptName);

            // And load in again and compare.
            StackHashScriptSettings loadedSettings = StackHashScriptSettings.Load(testScriptName);


            Assert.AreEqual(scriptSettings.CreationDate, loadedSettings.CreationDate);
            Assert.AreEqual(scriptSettings.LastModifiedDate, loadedSettings.LastModifiedDate);
            Assert.AreEqual(scriptSettings.Name, loadedSettings.Name);
            Assert.AreEqual(scriptSettings.Script.Count, loadedSettings.Script.Count);
            Assert.AreEqual(scriptSettings.RunAutomatically, loadedSettings.RunAutomatically);
            Assert.AreEqual(scriptSettings.Version, loadedSettings.Version);
            Assert.AreEqual(scriptSettings.Owner, loadedSettings.Owner);

            for (int i = 0; i < scriptSettings.Script.Count; i++)
            {
                Assert.AreEqual(scriptSettings.Script[i].Command, loadedSettings.Script[i].Command);
                Assert.AreEqual(scriptSettings.Script[i].Comment, loadedSettings.Script[i].Comment);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the specified script file.
        /// </summary>
        /// <param name="scriptName">Script to load.</param>
        /// <returns>The script settings.</returns>
        public StackHashScriptSettings LoadScript(string scriptName, bool noException)
        {
            String fileName = getScriptFileName(scriptName);

            if (!File.Exists(fileName))
            {
                if (noException)
                {
                    return(null);
                }
                else
                {
                    throw new StackHashException("Script file does not exist when loading script.", StackHashServiceErrorCode.ScriptDoesNotExist);
                }
            }

            StackHashScriptSettings newScriptSettings = StackHashScriptSettings.Load(fileName);

            return(newScriptSettings);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Renames the specified script.
        /// </summary>
        /// <param name="oldScriptName">Current name of the script.</param>
        /// <param name="newScriptName">New name of the script.</param>
        public void RenameScript(String oldScriptName, String newScriptName)
        {
            if (string.IsNullOrEmpty(oldScriptName))
            {
                throw new ArgumentNullException("oldScriptName");
            }
            if (string.IsNullOrEmpty(newScriptName))
            {
                throw new ArgumentNullException("newScriptName");
            }

            // Don't do anything if the name hasn't changed.
            if (oldScriptName == newScriptName)
            {
                return;
            }

            String fileName = getScriptFileName(oldScriptName);

            if (!File.Exists(fileName))
            {
                throw new StackHashException("Script name does not exist during rename", StackHashServiceErrorCode.ScriptDoesNotExist);
            }

            // Load in the script and save it to the new name.
            StackHashScriptSettings thisScript = StackHashScriptSettings.Load(fileName);

            thisScript.Name             = newScriptName;
            thisScript.LastModifiedDate = DateTime.Now.ToUniversalTime();

            String newFileName = getScriptFileName(newScriptName);

            thisScript.Save(newFileName);

            // Delete the old script as the renamed version is now available.
            File.Delete(fileName);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Add a new script.
        /// </summary>
        /// <param name="script">Full script settings.</param>
        /// <param name="overwrite">true - Overwrite any old script of the same name.</param>
        /// <param name="allowReadOnlyScriptOverwrite">true - allow read only script to be overwritten.</param>
        public void AddScript(StackHashScriptSettings script, bool overwrite, bool allowReadOnlyScriptOverwrite)
        {
            if (script == null)
            {
                throw new ArgumentNullException("script");
            }

            String fileName = getScriptFileName(script.Name);

            if (File.Exists(fileName))
            {
                if (!overwrite)
                {
                    throw new StackHashException("Script file already exists and overWrite not set", StackHashServiceErrorCode.CannotOverwriteScriptFile);
                }
                else
                {
                    // File already exists. The Created time should remain the same. The update time should change.
                    StackHashScriptSettings oldScript = StackHashScriptSettings.Load(fileName);

                    if (oldScript.IsReadOnly && !allowReadOnlyScriptOverwrite)
                    {
                        throw new InvalidOperationException("Cannot overwrite Read Only script");
                    }

                    script.CreationDate     = oldScript.CreationDate;
                    script.LastModifiedDate = DateTime.Now.ToUniversalTime();
                }
            }
            else
            {
                script.CreationDate     = DateTime.Now.ToUniversalTime();
                script.LastModifiedDate = script.CreationDate;
            }

            script.Save(fileName);
        }