Exemplo n.º 1
0
 private void ScriptRepositoryListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
 {
     if (((FrameworkElement)e.OriginalSource).DataContext is LawnchairMetadata)
     {
         LawnchairMetadata metadata = ((FrameworkElement)e.OriginalSource).DataContext as LawnchairMetadata;
         ExecuteScript(metadata);
     }
 }
Exemplo n.º 2
0
 private void scriptRepositoryListView_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Enter)
     {
         if (((FrameworkElement)e.OriginalSource).DataContext is LawnchairMetadata)
         {
             LawnchairMetadata metadata = ((FrameworkElement)e.OriginalSource).DataContext as LawnchairMetadata;
             ExecuteScript(metadata);
         }
     }
 }
Exemplo n.º 3
0
        private Boolean IsMetadataMatch(LawnchairMetadata metadata, String query)
        {
            bool match = false;        // Default value

            if (null != metadata.Tags) // If tags are present
            {
                if (metadata.Author.Contains(query, StringComparison.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to Author [" + metadata.Author + "]");
                    match = true;
                }
                if (metadata.Category.Contains(query, StringComparison.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to Category [" + metadata.Category + "]");
                    match = true;
                }
                if (metadata.Name.Contains(query, StringComparison.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to Name [" + metadata.Name + "]");
                    match = true;
                }
                if (metadata.Tags.Contains(query, StringComparer.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to one of the Tags [" + metadata.Tags.Where(o => o == query).Last() + "]");
                    match = true;
                }
            }
            else
            {
                if (metadata.Author.Contains(query, StringComparison.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to Author [" + metadata.Author + "]");
                    match = true;
                }
                if (metadata.Category.Contains(query, StringComparison.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to Category [" + metadata.Category + "]");
                    match = true;
                }
                if (metadata.Name.Contains(query, StringComparison.OrdinalIgnoreCase))
                {
                    AppendToDebugOutput("Matched [" + query + "] to Name [" + metadata.Name + "]");
                    match = true;
                }
            }

            return(match);
        }
Exemplo n.º 4
0
        private void ExecuteScript(LawnchairMetadata metadata)
        {
            try
            {
                String           scriptFullPath = metadata.ScriptRootPath + metadata.ScriptRelativePath;
                MessageBoxResult response       = MessageBox.Show(
                    metadata.Name + Environment.NewLine +
                    "----------------" + Environment.NewLine +
                    "Author comments:" + Environment.NewLine + Environment.NewLine +
                    metadata.Comments + Environment.NewLine + Environment.NewLine +
                    "----------------" + Environment.NewLine +
                    "Click OK to run this script otherwise click CANCEL to return to the previous screen.",
                    "Run Script?",
                    MessageBoxButton.OKCancel);

                if (response == MessageBoxResult.OK)
                {
                    String scriptArguments = metadata.ScriptArguments.Replace("@scriptFullPath", scriptFullPath);
                    AppendToDebugOutput("Calling script executor [" + metadata.ScriptExecutor + "]");
                    AppendToDebugOutput(metadata.ScriptExecutor + " " + scriptArguments);
                    Process process = new Process
                    {
                        StartInfo = new ProcessStartInfo()
                        {
                            Arguments       = scriptArguments,
                            FileName        = metadata.ScriptExecutor,
                            UseShellExecute = true,
                        }
                    };
                    process.Start();
                    process.WaitForExit();
                }
            }
            catch (Exception exception)
            {
                HandleException(exception);
            }
        }
Exemplo n.º 5
0
        private List <LawnchairMetadata> GetScriptRepositoryMetadataFiles(String scriptMetadataFilename, String scriptRepositoryPath)
        {
            List <LawnchairMetadata> metadataList = new List <LawnchairMetadata>();

            foreach (String metadataFile in Directory.EnumerateFiles(scriptRepositoryPath, settings.ScriptMetadataFilename, SearchOption.AllDirectories))
            {
                try
                {
                    // Get the metadata JSON content as a string
                    AppendToDebugOutput("Found " + settings.ScriptMetadataFilename + " at [" + metadataFile + "]");
                    String json = File.ReadAllText(metadataFile);
                    AppendToDebugOutput("Metadata file contents:");
                    AppendToDebugOutput(json);

                    // Replace placeholder content in the JSON string
                    FileInfo metadataFileInfo = new FileInfo(metadataFile);
                    String   folderPath       = metadataFileInfo.Directory.FullName;
                    // If the JSON contains @category, replace with the name of the metadata file's parent folder. If the category value needs to be something else then the author can replace the @category in the JSON with a hardcoded category name
                    if (json.Contains("@category"))
                    {
                        String category = folderPath.Replace(scriptRepositoryPath + @"\", null);
                        json = json.Replace("\"@category\"", JsonConvert.ToString(category));
                    }

                    // If the JSON contains @scriptRootPath, replace with the name of the metadata file's parent folder's absolute path. If the scriptRootPath value needs to be something else then the author can replace the @scriptRootPath in the JSON with a hardcoded absolute path to the folder the script resides in
                    if (json.Contains("@scriptRootPath"))
                    {
                        json = json.Replace("\"@scriptRootPath\"", JsonConvert.ToString(folderPath));
                    }
                    AppendToDebugOutput("JSON after formatting and variable expansions:");
                    AppendToDebugOutput(json);
                    if (json.Trim().First() == '[' && json.Trim().Last() == ']')
                    {
                        AppendToDebugOutput("JSON appears to have multiple sets of metadata");
                        foreach (LawnchairMetadata metadata in JsonConvert.DeserializeObject <List <LawnchairMetadata> >(json))
                        {
                            metadataList.Add(metadata);
                        }
                    }
                    else
                    {
                        LawnchairMetadata metadata = JsonConvert.DeserializeObject <LawnchairMetadata>(json);
                        metadataList.Add(metadata);
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(
                        "An error occurred while processing the metadata from " + "[" + metadataFile + "]." + Environment.NewLine + Environment.NewLine +
                        "Generally, this means that a script has a badly formatted metadata file. We'll have to skip this script and it will not show in the list until that script's author corrects this problem." + Environment.NewLine + Environment.NewLine +
                        "If you are the script author, more information on the specific exception can be found in the debug panel, accessible by pressing CTRL + D after dismissing this message.",
                        "Error");
                    AppendToDebugOutput("Exception thrown:");
                    AppendToDebugOutput(exception.ToString());
                }
            }

            if (metadataList.Count == 0 && Directory.EnumerateFiles(scriptRepositoryPath, settings.ScriptMetadataFilename, SearchOption.AllDirectories).Count() > 0)
            {
                throw new Exception("Metadata files were found (recursively) under the starting root path [" + scriptRepositoryPath + "] but none were successfully loaded. Though it is possible all metadata files are incorrectly formatted the more likely scenario is an access problem to the scriptRepositoryPath or some other problem with this applications code.");
            }

            // Return to caller
            return(metadataList);
        }