Beispiel #1
0
 public void Can_fetch_locations_in_the_cache()
 {
     var type = DefinitionCacheItemType.Script;
     var cache = new DefinitionCache();
     cache
         .Add(type, "loc1", DateTime.Now, false, true, "cmd1", "")
             .Append(type, "loc1", DateTime.Now, false, true, "cmd2", "")
                 .Append(DefinitionCacheItemType.Language, "loc3", new DateTime(2012,10,1,0,0,0), false, true, "cmd3", "")
                     .Append(type, "loc2", new DateTime(2012,1,1,0,0,0), false, false, "-g", "");
     Assert.That(cache.GetLocations(DefinitionCacheItemType.Script).Length, Is.EqualTo(2));
 }
Beispiel #2
0
 public void Can_fetch_the_oldest_element_of_a_spesific_location_in_the_cache()
 {
     var type = DefinitionCacheItemType.Script;
     var cache = new DefinitionCache();
     cache
         .Add(type, "loc1", DateTime.Now, false, true, "cmd1", "")
             .Append(type, "loc1", DateTime.Now, false, true, "cmd2", "")
                 .Append(type, "loc1", new DateTime(2012,10,1,0,0,0), false, true, "cmd3", "")
                     .Append(type, "loc2", new DateTime(2012,1,1,0,0,0), false, false, "-g", "");
     Assert.That(cache.GetOldestItem("loc1").Name, Is.EqualTo("cmd3"));
 }
 public void Write(DefinitionCache cache)
 {
     var file = Path.Combine(_path, "oi-definitions.json");
     var json = new List<DefinitionJson>();
     cache.Definitions.OrderBy(x => x.Override).ToList()
         .ForEach(x => json.Add(get(x)));
     _writer(
         file,
         LowercaseJsonSerializer
             .SerializeObject(new DefinedCommands() { Commands = json }));
 }
Beispiel #4
0
 public void Can_find_added_command()
 {
     var type = DefinitionCacheItemType.Script;
     var time = DateTime.Now;
     var cache = new DefinitionCache();
     cache
         .Add(type, "", time, false, true, "cmd1", "")
             .Append(type, "", time, false, true, "cmd2", "")
                 .Append(type, "", time, false, true, "cmd3", "");
     Assert.That(cache.Get(new[] {"cmd1","cmd2"}).Name, Is.EqualTo("cmd2"));
 }
 public DefinitionCache Read(string file)
 {
     try {
         var cache = new DefinitionCache();
         var json = JObject.Parse(_readFile(file));
         foreach (var cmd in json["commands"].Children())
             addItem(cache, cmd);
         return cache;
     } catch (Exception ex) {
         Logger.Write(ex);
     }
     return null;
 }
Beispiel #6
0
        public void Build()
        {
            _cache = new DefinitionCache();
            var profiles = new ProfileLocator(_token);

            mergeBuiltInCommands(profiles);
            // Reverse paths to handle global first then local
            var paths = profiles.GetPathsCurrentProfiles().Reverse().ToArray();
            //for (int i = paths.Length - 1; i >= 0; i--)
            //	mergeExternalCommands(paths[i]);
            foreach (var path in paths)
                mergeExternalCommands(path);
        }
Beispiel #7
0
 public void Can_get_built_in_command_hidden_by_override()
 {
     var cache = new DefinitionCache();
     cache
         .Add(DefinitionCacheItemType.BuiltIn, "l1", DateTime.Now, false, true, "cmd1", "")
             .Append(DefinitionCacheItemType.BuiltIn, "l1", DateTime.Now, false, true, "cmd1-1", "");
     cache
         .Add(DefinitionCacheItemType.Script, "l2", DateTime.Now, false, true, "cmd1", "")
             .Append(DefinitionCacheItemType.Script, "l2", DateTime.Now, false, true, "override-1", "");
     var item = cache.GetBuiltIn(new[] { "cmd1" });
     Assert.That(item.Type, Is.EqualTo(DefinitionCacheItemType.BuiltIn));
     Assert.That(item.Location, Is.EqualTo("l1"));
     Assert.That(item.Parameters[0].Name, Is.EqualTo("cmd1-1"));
 }
Beispiel #8
0
 public void Can_fetch_the_oldest_element_in_the_cache()
 {
     var type = DefinitionCacheItemType.Script;
     var time = new DateTime(2012,1,1,0,0,0);
     var cache = new DefinitionCache();
     cache
         .Add(type, "", DateTime.Now, false, true, "cmd1", "")
             .Append(type, "", DateTime.Now, false, true, "cmd2", "")
                 .Append(type, "", DateTime.Now, false, true, "cmd3", "")
                     .Append(type, "", time, false, false, "-g", "");
     cache
         .Add(type, "", DateTime.Now, false, true, "cmd1-1", "")
             .Append(type, "", DateTime.Now, false, true, "cmdAnother", "");
     Assert.That(cache.GetOldestItem().Name, Is.EqualTo("-g"));
 }
 private void addItem(DefinitionCache cache, JToken json)
 {
     var item =
         cache
             .Add(
                 getType(json["type"].ToString()),
                 json["location"].ToString(),
                 getTime(json["updated"].ToString()),
                 json["cmd"].ToString().StartsWith("[["),
                 !json["cmd"].ToString().StartsWith("["),
                 json["cmd"].ToString().Replace("[", "").Replace("]", ""),
                 json["description"].ToString());
     foreach (var child in json["arguments"].Children())
         addItem(item, child);
 }
Beispiel #10
0
        private void add(DefinitionCache cache, DefinitionCacheItem item, DefinitionCacheItem parameter)
        {
            var name  = parameter.Name;
            var child =
                item.Append(
                    item.Type,
                    item.Location,
                    item.Updated,
                    parameter.Override,
                    parameter.Required,
                    name,
                    parameter.Description);

            foreach (var cmd in parameter.Parameters)
            {
                add(cache, child, cmd);
            }
        }
        public void Can_write_definition_cache()
        {
            string actualJSON = null;
            var writer =
                new DefinitionCacheWriter("/write/path")
                    .WriteUsing((file,content) => actualJSON = content);

            var type = DefinitionCacheItemType.Script;
            var script = "/my/script";
            var time = new DateTime(2013,1,1,2,3,1);
            var cache = new DefinitionCache();
            cache
                .Add(type, script, time, false, true, "mycmd", "My command does my stuff.")
                    .Append(type, script, time, false, true, "FILE", "another param")
                        .Append(type, script, time, false, false, "optional", "This one is optional");

            writer.Write(cache);
            Assert.That(actualJSON, Is.EqualTo(expectedJSON()));
        }
Beispiel #12
0
        private DefinitionCache writeBuiltInCommands(string file, ProfileLocator profiles)
        {
            var builtInCache = new DefinitionCache();

            foreach (var cmd in _builtIn())
            {
                var item = builtInCache
                           .Add(
                    DefinitionCacheItemType.BuiltIn,
                    null,
                    DateTime.Now,
                    false,
                    true,
                    cmd.Name,
                    cmd.Usage.Description);
                add(builtInCache, item, cmd.Usage.Parameters);
            }
            writeCache(profiles.AppRootPath, builtInCache);
            return(builtInCache);
        }
Beispiel #13
0
        private void mergeBuiltInCommands(bool updateDefinitions, ProfileLocator profiles)
        {
            var builtInFile  = Path.Combine(profiles.AppRootPath, "oi-definitions.json");
            var builtInCache = new DefinitionCache();

            if (File.Exists(builtInFile))
            {
                builtInCache = appendDefinitions(builtInFile);
                var updated = builtInCache.GetOldestItem();
                if (updateDefinitions && updated != null && fileTime(System.Reflection.Assembly.GetExecutingAssembly().Location) > updated.Updated)
                {
                    builtInCache = writeBuiltInCommands(builtInFile, profiles);
                }
            }
            else
            {
                if (updateDefinitions)
                {
                    Logger.Write("Could not find definition file: " + builtInFile);
                    builtInCache = writeBuiltInCommands(builtInFile, profiles);
                }
            }
            _cache.Merge(_enabledLanguages, builtInCache);
        }
Beispiel #14
0
 public void Merge(string[] enabledLanguages, DefinitionCache cache)
 {
     foreach (var definition in cache.Definitions)
     {
         Logger.Write("merging " + definition.Name + " having override " + definition.Override.ToString());
         // Skip not enabled languages
         // We can handle this on merge as
         // 1. Writing / updating definition files happens before merge
         // 2. The first part being written to the cache is builtin
         //    which contains no languages
         if (!definition.Override && definition.Type == DefinitionCacheItemType.Language && !enabledLanguages.Contains(definition.Name))
         {
             continue;
         }
         if (definition.Override)
         {
             overrideItem(definition);
         }
         else
         {
             add(definition);
         }
     }
 }
Beispiel #15
0
 private void add(DefinitionCache cache, DefinitionCacheItem item, BaseCommandHandlerParameter parameter)
 {
     var name = parameter.Name;
     var child =
         item.Append(
                 item.Type,
                 item.Location,
                 item.Updated,
                 parameter.Override,
                 parameter.Required,
                 name,
                 parameter.Description);
     foreach (var cmd in parameter.Parameters)
         add(cache, child, cmd);
 }
Beispiel #16
0
        private DefinitionCache buildDefinitions(string file)
        {
            var cache = new DefinitionCache();
            var dir = Path.GetDirectoryName(file);

            // Add languages
            var languagePath = Path.Combine(dir, "languages");
            LanguagePlugin defaultLanguage = null;
            foreach (var language in _languages(languagePath)) {
                var item = cache.Add(
                    DefinitionCacheItemType.Language,
                    language.FullPath,
                    DateTime.Now,
                    false,
                    true,
                    language.GetLanguage(),
                    "Commands for the " + language.GetLanguage() + " plugin");
                add(cache, item, language.GetUsages());

                var languageScriptPath =
                    Path.Combine(
                        Path.Combine(
                            languagePath, language.GetLanguage() + "-files"),
                            "scripts");
                Logger.Write("Adding scripts from " + languageScriptPath);
                var languageScripts = new ScriptFilter().GetScripts(languageScriptPath);
                foreach (var scriptFile in languageScripts) {
                    Logger.Write("Script " + scriptFile);
                    var script  = new Script(_token, _workingDirectory, scriptFile);
                    var usages = script.Usages; // Description is built when fetching usages
                    var scriptItem = item.Append(
                        DefinitionCacheItemType.LanguageScript,
                        scriptFile,
                        DateTime.Now,
                        false,
                        true,
                        script.Name,
                        script.Description);
                    add(cache, scriptItem, usages);
                }

                if (language.GetLanguage() == _defaultLanguage)
                    defaultLanguage = language;
            }

            // Add scripts
            var scriptPath = Path.Combine(dir, "scripts");
            Logger.Write("Adding scripts from " + scriptPath);
            var scripts = new ScriptFilter().GetScripts(scriptPath);
            foreach (var scriptFile in scripts) {
                Logger.Write("Adding script " + scriptPath);
                var script  = new Script(_token, _workingDirectory, scriptFile);
                var usages = script.Usages; // Description is built when fetching usages
                var item = cache.Add(
                    DefinitionCacheItemType.Script,
                    scriptFile,
                    DateTime.Now,
                    false,
                    true,
                    script.Name,
                    script.Description);
                add(cache, item, usages);
            }

            // Add default language
            if (defaultLanguage != null) {
                var parameters = cache.Get(new[] { defaultLanguage.GetLanguage() }).Parameters;
                foreach (var usage in parameters) {
                    // Don't override existing commands with default language
                    if (cache.Get(new[] { usage.Name }) == null) {
                        var item = cache.Add(
                            usage.Type,
                            usage.Location,
                            DateTime.Now,
                            false,
                            true,
                            usage.Name,
                            usage.Description);
                        add(cache, item, usage.Parameters);
                    }
                }
            }
            writeCache(dir, cache);
            return cache;
        }
Beispiel #17
0
 public void When_merging_two_definition_caches_it_will_not_add_duplicate_commands()
 {
     var type = DefinitionCacheItemType.Script;
     var time = DateTime.Now;
     var cache = new DefinitionCache();
     cache
         .Add(type, "", time, false, true, "cmd1", "")
             .Append(type, "", time, false, true, "cmd2", "")
                 .Append(type, "", time, false, true, "cmd3", "")
                     .Append(type, "", time, false, false, "-g", "");
     var another = new DefinitionCache();
     another
         .Add(type, "", time, false, true, "cmd1", "")
             .Append(type, "", time, false, true, "cmdAnother", "");
     cache.Merge(another);
     Assert.That(cache.Definitions.Length, Is.EqualTo(1));
 }
Beispiel #18
0
 private DefinitionCache writeBuiltInCommands(string file, ProfileLocator profiles)
 {
     var builtInCache = new DefinitionCache();
     foreach (var cmd in _builtIn()) {
         var item = builtInCache
             .Add(
                 DefinitionCacheItemType.BuiltIn,
                 null,
                 DateTime.Now,
                 false,
                 true,
                 cmd.Name,
                 cmd.Usage.Description);
         add(builtInCache, item, cmd.Usage.Parameters);
     }
     writeCache(profiles.AppRootPath, builtInCache);
     return builtInCache;
 }
Beispiel #19
0
 public void Merge(DefinitionCache cache)
 {
     foreach (var definition in cache.Definitions) {
         if (definition.Override)
             overrideItem(definition);
         else
             add(definition);
     }
 }
 private string[] replacePlaceholderLanguages(string path, DefinitionCache.DefinitionLocation[] locations)
 {
     var result = new List<string>();
     foreach (var location in locations) {
         if (location.Location == "placehoder-for-language-in-different-location")
             result.Add(Path.Combine(path, location.Name));
         else
             result.Add(location.Location);
     }
     return result.ToArray();
 }
Beispiel #21
0
 public void Can_merge_two_definition_caches()
 {
     var type = DefinitionCacheItemType.Script;
     var time = DateTime.Now;
     var cache = new DefinitionCache();
     cache
         .Add(type, "", time, false, true, "cmd1", "")
             .Append(type, "", time, false, true, "cmd2", "")
                 .Append(type, "", time, false, true, "cmd3", "")
                     .Append(type, "", time, false, false, "-g", "");
     var another = new DefinitionCache();
     another
         .Add(type, "", time, false, true, "cmdAnother", "")
             .Append(type, "", time, false, true, "cmdAnother2", "");
     cache.Merge(another);
     Assert.That(cache.Definitions.Length, Is.EqualTo(2));
 }
Beispiel #22
0
        private DefinitionCache buildDefinitions(string file)
        {
            var cache = new DefinitionCache();
            var dir   = Path.GetDirectoryName(file);

            // Add languages
            var            languagePath    = Path.Combine(dir, "languages");
            LanguagePlugin defaultLanguage = null;

            foreach (var language in _languages(languagePath))
            {
                var item = cache.Add(
                    DefinitionCacheItemType.Language,
                    language.FullPath,
                    DateTime.Now,
                    false,
                    true,
                    language.GetLanguage(),
                    "Commands for the " + language.GetLanguage() + " plugin");
                add(cache, item, language.GetUsages());
            }

            // Add language scripts
            var currentLanguages = cache
                                   .Definitions
                                   .Where(x => x.Type == DefinitionCacheItemType.Language)
                                   .ToList();
            var otherLocationLanguages = _cache
                                         .Definitions
                                         .Where(x => x.Type == DefinitionCacheItemType.Language && !currentLanguages.Any(y => y.Name == x.Name))
                                         .ToList();

            foreach (var item in currentLanguages)
            {
                var languageScriptPath =
                    Path.Combine(
                        Path.Combine(
                            languagePath, item.Name + "-files"),
                        "scripts");
                if (!Directory.Exists(languageScriptPath))
                {
                    continue;
                }
                Logger.Write("Adding scripts from " + languageScriptPath);
                var languageScripts = new ScriptFilter().GetScripts(languageScriptPath);
                foreach (var scriptFile in languageScripts)
                {
                    Logger.Write("Script " + scriptFile);
                    var script = new Script(_token, _workingDirectory, scriptFile)
                                 .SetUsageDispatcher((msg) => {
                        if (msg.StartsWith("error|"))
                        {
                            printError(msg);
                        }
                    });
                    var usages     = script.Usages;                 // Description is built when fetching usages
                    var scriptItem = item.Append(
                        DefinitionCacheItemType.LanguageScript,
                        scriptFile,
                        DateTime.Now,
                        false,
                        true,
                        script.Name,
                        script.Description);
                    add(cache, scriptItem, usages);
                }
            }
            foreach (var language in otherLocationLanguages)
            {
                var languageScriptPath =
                    Path.Combine(
                        Path.Combine(
                            languagePath, language.Name + "-files"),
                        "scripts");
                if (!Directory.Exists(languageScriptPath))
                {
                    continue;
                }
                var item = cache.Add(
                    DefinitionCacheItemType.Language,
                    "placehoder-for-language-in-different-location",
                    DateTime.Now,
                    false,
                    true,
                    language.Name,
                    "");
                add(cache, item, new BaseCommandHandlerParameter[] {});
                Logger.Write("Adding scripts from " + languageScriptPath);
                var languageScripts = new ScriptFilter().GetScripts(languageScriptPath);
                foreach (var scriptFile in languageScripts)
                {
                    Logger.Write("Script " + scriptFile);
                    var script = new Script(_token, _workingDirectory, scriptFile)
                                 .SetUsageDispatcher((msg) => {
                        if (msg.StartsWith("error|"))
                        {
                            printError(msg);
                        }
                    });
                    var usages     = script.Usages;                 // Description is built when fetching usages
                    var scriptItem = item.Append(
                        DefinitionCacheItemType.LanguageScript,
                        scriptFile,
                        DateTime.Now,
                        false,
                        true,
                        script.Name,
                        script.Description);
                    add(cache, scriptItem, usages);
                }
            }

            // Add scripts
            var scriptPath = Path.Combine(dir, "scripts");

            Logger.Write("Adding scripts from " + scriptPath);
            var scripts = new ScriptFilter().GetScripts(scriptPath);

            foreach (var scriptFile in scripts)
            {
                Logger.Write("Adding script " + scriptPath);
                var script = new Script(_token, _workingDirectory, scriptFile)
                             .SetUsageDispatcher((msg) => {
                    if (msg.StartsWith("error|"))
                    {
                        printError(msg);
                    }
                });
                var usages = script.Usages;                 // Description is built when fetching usages
                var item   = cache.Add(
                    DefinitionCacheItemType.Script,
                    scriptFile,
                    DateTime.Now,
                    false,
                    true,
                    script.Name,
                    script.Description);
                add(cache, item, usages);
            }

            writeCache(dir, cache);
            return(cache);
        }
Beispiel #23
0
 private void writeCache(string path, DefinitionCache cache)
 {
     new DefinitionCacheWriter(path)
     .Write(cache);
 }
Beispiel #24
0
        private bool isUpdated(string file, DefinitionCache cache)
        {
            var updated = cache.GetOldestItem(file).Updated;
            var filetime = fileTime(file);
            if (filetime > updated) {
                Logger.Write("Oldest is {0} {1} for {2}", updated.ToShortDateString(), updated.ToLongTimeString(), file);
                Logger.Write("Definition is out of date for {0} with file time {1} {2}", file, filetime.ToShortDateString(), filetime.ToLongTimeString());
                return true;
            }

            var dir = Path.GetDirectoryName(file);
            var name = Path.GetFileNameWithoutExtension(file);
            var filesDir = Path.Combine(dir, name + "-files");
            if (Directory.Exists(filesDir)) {
                return isUpdated(updated, filesDir, Path.Combine(filesDir, "state"));
            }
            return false;
        }
Beispiel #25
0
 private void mergeBuiltInCommands(ProfileLocator profiles)
 {
     var builtInFile = Path.Combine(profiles.AppRootPath, "oi-definitions.json");
     var builtInCache = new DefinitionCache();
     if (File.Exists(builtInFile)) {
         builtInCache = appendDefinitions(builtInFile);
         var updated = builtInCache.GetOldestItem();
         if (updated != null && fileTime(System.Reflection.Assembly.GetExecutingAssembly().Location) > updated.Updated)
             builtInCache = writeBuiltInCommands(builtInFile, profiles);
     } else {
         Logger.Write("Could not find definition file: " + builtInFile);
         builtInCache = writeBuiltInCommands(builtInFile, profiles);
     }
     _cache.Merge(builtInCache);
 }
 public void Merge(string[] enabledLanguages, DefinitionCache cache)
 {
     foreach (var definition in cache.Definitions) {
         Logger.Write("merging "+definition.Name+" having override "+definition.Override.ToString());
         // Skip not enabled languages
         // We can handle this on merge as
         // 1. Writing / updating definition files happens before merge
         // 2. The first part being written to the cache is builtin
         //    which contains no languages
         if (!definition.Override && definition.Type == DefinitionCacheItemType.Language && !enabledLanguages.Contains(definition.Name))
             continue;
         if (definition.Override)
             overrideItem(definition);
         else
             add(definition);
     }
 }
Beispiel #27
0
        private void mergeExternalCommands(string path)
        {
            Logger.Write("Merging path " + path);
            var localCache = new DefinitionCache();
            var file = Path.Combine(path, "oi-definitions.json");
            if (File.Exists(file)) {
                localCache = appendDefinitions(file);
                if (cacheIsOutOfDate(file, localCache)) {
                    Logger.Write("Definition file was out of date, updating " + file);
                    localCache = buildDefinitions(file);
                }
            } else {
                Logger.Write("Could not find definition file: " + file);
                localCache = buildDefinitions(file);
            }

            _cache.Merge(localCache);
        }
Beispiel #28
0
 private void writeCache(string path, DefinitionCache cache)
 {
     new DefinitionCacheWriter(path)
         .Write(cache);
 }
Beispiel #29
0
 private void add(DefinitionCache cache, DefinitionCacheItem item, IEnumerable<BaseCommandHandlerParameter> parameters)
 {
     foreach (var parameter in parameters) {
         if (parameter.Override)
             overrideCommand(cache, item, parameter);
         else
             add(cache, item, parameter);
     }
 }
Beispiel #30
0
 private void add(DefinitionCache cache, DefinitionCacheItem item, IEnumerable<DefinitionCacheItem> parameters)
 {
     foreach (var parameter in parameters)
         add(cache, item, parameter);
 }
Beispiel #31
0
        private bool cacheIsOutOfDate(string file, DefinitionCache cache)
        {
            try {
                var dir        = Path.GetDirectoryName(file);
                var locations  = cache.GetLocations(DefinitionCacheItemType.Script).Select(x => x.Location);
                var scriptPath = Path.Combine(dir, "scripts");
                var scripts    = new ScriptFilter().GetScripts(scriptPath);
                if (scripts.Any(x => !locations.Contains(x)))
                {
                    Logger.Write("New script has been added");
                    return(true);
                }
                if (locations.Any(x => !scripts.Contains(x)))
                {
                    Logger.Write("New script has been added");
                    return(true);
                }

                foreach (var script in scripts)
                {
                    if (isUpdated(script, cache))
                    {
                        return(true);
                    }
                }

                var languagePath = Path.Combine(dir, "languages");
                var rawLocations = cache.GetLocations(DefinitionCacheItemType.Language);
                locations = replacePlaceholderLanguages(languagePath, rawLocations);
                var languages = _languages(languagePath).Select(x => x.FullPath).ToList();
                if (languages.Any(x => !locations.Contains(x)))
                {
                    Logger.Write("New language has been added");
                    if (Logger.IsEnabled)
                    {
                        foreach (var newLanguage in languages.Where(x => !locations.Contains(x)))
                        {
                            Logger.Write("\t" + newLanguage);
                        }
                    }
                    return(true);
                }
                if (locations.Any(x => !languages.Any(y => y == x)))
                {
                    Logger.Write("Language has been removed");
                    return(true);
                }

                languages = addPlaceholderLanguages(languagePath, languages);
                foreach (var language in languages)
                {
                    if (isUpdated(language, cache))
                    {
                        return(true);
                    }
                    var languageScriptPath =
                        Path.Combine(
                            Path.Combine(languagePath, Path.GetFileNameWithoutExtension(language) + "-files"),
                            "scripts");
                    if (Directory.Exists(languageScriptPath))
                    {
                        locations = cache
                                    .GetLocations(DefinitionCacheItemType.LanguageScript)
                                    .Select(x => x.Location)
                                    .Where(x => x.StartsWith(languageScriptPath))
                                    .ToArray();
                        var languageScripts = new ScriptFilter().GetScripts(languageScriptPath);
                        if (languageScripts.Any(x => !locations.Contains(x)))
                        {
                            Logger.Write("Language script has been added");
                            return(true);
                        }
                        if (locations.Any(x => !languageScripts.Contains(x)))
                        {
                            Logger.Write("Language script has been removed");
                            return(true);
                        }
                        foreach (var script in languageScripts)
                        {
                            if (isUpdated(script, cache))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }  catch(Exception ex)
            {
                Logger.Write(ex.ToString());
                return(true);
            }
            return(false);
        }
        public void Build(bool updateDefinitions)
        {
            _cache = new DefinitionCache();
            var profiles = new ProfileLocator(_token);

            mergeBuiltInCommands(updateDefinitions, profiles);
            // Reverse paths to handle global first then local
            var paths = profiles.GetPathsCurrentProfiles().Reverse().ToArray();
            foreach (var path in paths)
                mergeExternalCommands(updateDefinitions, path);

            // Add default language
            if (_defaultLanguage != null) {
                var lang = _cache.Get(new[] { _defaultLanguage });
                if (lang != null) {
                    var parameters = lang.Parameters;
                    foreach (var usage in parameters) {
                        // Don't override existing commands with default language
                        if (_cache.Get(new[] { usage.Name }) == null) {
                            _cache.Add(usage);
                        }
                    }
                }
            }
        }
Beispiel #33
0
 public void Stops_at_first_optional_argument()
 {
     var type = DefinitionCacheItemType.Script;
     var time = DateTime.Now;
     var cache = new DefinitionCache();
     cache
         .Add(type, "", time, false, true, "cmd1", "")
             .Append(type, "", time, false, true, "cmd2", "")
                 .Append(type, "", time, false, true, "cmd3", "")
                     .Append(type, "", time, false, false, "-g", "");
     Assert.That(cache.Get(new[] {"cmd1", "-g","cmd2","cmd3"}).Name, Is.EqualTo("cmd1"));
 }
        private bool cacheIsOutOfDate(string file, DefinitionCache cache)
        {
            try {
                var dir = Path.GetDirectoryName(file);
                var locations = cache.GetLocations(DefinitionCacheItemType.Script).Select(x => x.Location);
                var scriptPath = Path.Combine(dir, "scripts");
                var scripts = new ScriptFilter().GetScripts(scriptPath);
                if (scripts.Any(x => !locations.Contains(x))) {
                    Logger.Write("New script has been added");
                    return true;
                }
                if (locations.Any(x => !scripts.Contains(x))) {
                    Logger.Write("New script has been added");
                    return true;
                }

                foreach (var script in scripts) {
                    if (isUpdated(script, cache))
                        return true;
                }

                var languagePath = Path.Combine(dir, "languages");
                var rawLocations = cache.GetLocations(DefinitionCacheItemType.Language);
                locations = replacePlaceholderLanguages(languagePath, rawLocations);
                var languages = _languages(languagePath).Select(x => x.FullPath).ToList();
                if (languages.Any(x => !locations.Contains(x))) {
                    Logger.Write("New language has been added");
                    if (Logger.IsEnabled) {
                        foreach (var newLanguage in languages.Where(x => !locations.Contains(x)))
                            Logger.Write("\t" + newLanguage);
                    }
                    return true;
                }
                var removedLangs = locations.Where(x => !languages.Any(y => y == x));
                if (removedLangs.Count() > 0) {
                    foreach (var rmLang in removedLangs) {
                        Logger.Write("Language {0} removed?", rmLang);
                        if (!File.Exists(rmLang)) {
                            Logger.Write("Language {0} has been removed", rmLang);
                            return true;
                        } else {
                            Logger.Write("found language {0} on disk, continuing", rmLang);
                        }
                    }
                }

                languages = addPlaceholderLanguages(languagePath, languages);
                foreach (var language in languages) {
                    if (isUpdated(language, cache))
                        return true;
                    var languageScriptPath =
                        Path.Combine(
                            Path.Combine(languagePath, Path.GetFileNameWithoutExtension(language) + "-files"),
                            "scripts");
                    if (Directory.Exists(languageScriptPath)) {
                        locations = cache
                            .GetLocations(DefinitionCacheItemType.LanguageScript)
                            .Select(x => x.Location)
                            .Where(x => x.StartsWith(languageScriptPath))
                            .ToArray();
                        var languageScripts = new ScriptFilter().GetScripts(languageScriptPath);
                        if (languageScripts.Any(x => !locations.Contains(x))) {
                            Logger.Write("Language script has been added");
                            return true;
                        }
                        if (locations.Any(x => !languageScripts.Contains(x))) {
                            Logger.Write("Language script has been removed");
                            return true;
                        }
                        foreach (var script in languageScripts) {
                            if (isUpdated(script, cache))
                                return true;
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.Write(ex.ToString());
                return true;
            }
            return false;
        }
Beispiel #35
0
 public void Stops_at_first_user_defined_content_command()
 {
     var type = DefinitionCacheItemType.Script;
     var time = DateTime.Now;
     var cache = new DefinitionCache();
     cache
         .Add(type, "", time, false, true, "cmd1", "")
             .Append(type, "", time, false, true, "USER_DEFINED_ARE_UPPER_CASE", "")
                 .Append(type, "", time, false, true, "cmd3", "");
     Assert.That(cache.Get(new[] {"cmd1","bleh","cmd3"}).Name, Is.EqualTo("cmd1"));
 }
Beispiel #36
0
        private bool cacheIsOutOfDate(string file, DefinitionCache cache)
        {
            try {
                var dir = Path.GetDirectoryName(file);
                var locations = cache.GetLocations(DefinitionCacheItemType.Script);
                var scriptPath = Path.Combine(dir, "scripts");
                var scripts = new ScriptFilter().GetScripts(scriptPath);
                if (scripts.Any(x => !locations.Contains(x))) {
                    Logger.Write("New script has been added");
                    return true;
                }
                if (locations.Any(x => !scripts.Contains(x))) {
                    Logger.Write("New script has been added");
                    return true;
                }

                foreach (var script in scripts) {
                    if (isUpdated(script, cache))
                        return true;
                }

                locations = cache.GetLocations(DefinitionCacheItemType.Language);
                var languagePath = Path.Combine(dir, "languages");
                var languages = _languages(languagePath);
                if (languages.Any(x => !locations.Contains(x.FullPath))) {
                    Logger.Write("New language has been added");
                    if (Logger.IsEnabled) {
                        foreach (var newLanguage in languages.Where(x => !locations.Contains(x.FullPath)))
                            Logger.Write("\t" + newLanguage.FullPath);
                    }
                    return true;
                }
                if (locations.Any(x => !languages.Any(y => y.FullPath == x))) {
                    Logger.Write("Language has been removed");
                    return true;
                }

                foreach (var language in languages) {
                    if (isUpdated(language.FullPath, cache))
                        return true;
                    var languageScriptPath =
                        Path.Combine(
                            Path.Combine(languagePath, language.GetLanguage() + "-files"),
                            "scripts");
                    if (Directory.Exists(languageScriptPath)) {
                        locations = cache
                            .GetLocations(DefinitionCacheItemType.LanguageScript)
                            .Where(x => x.StartsWith(languageScriptPath))
                            .ToArray();
                        var languageScripts = new ScriptFilter().GetScripts(languageScriptPath);
                        if (languageScripts.Any(x => !locations.Contains(x))) {
                            Logger.Write("Language script has been added");
                            return true;
                        }
                        if (locations.Any(x => !languageScripts.Contains(x))) {
                            Logger.Write("Language script has been removed");
                            return true;
                        }
                        foreach (var script in languageScripts) {
                            if (isUpdated(script, cache))
                                return true;
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.Write(ex.ToString());
                return true;
            }
            return false;
        }
Beispiel #37
0
 private void overrideCommand(DefinitionCache cache, DefinitionCacheItem item, BaseCommandHandlerParameter parameter)
 {
     var command = cache.Add(
         item.Type,
         item.Location,
         item.Updated,
         parameter.Override,
         parameter.Required,
         parameter.Name,
         parameter.Description);
     foreach (var cmd in parameter.Parameters)
         add(cache, command, cmd);
 }