private static string GetSaveFilePath(ModelPaths modelPaths)
        {
            string dataFileName = $"{Path.GetFileNameWithoutExtension(modelPaths.ModelPath)}.dpnr";
            string folderPath   = Path.GetDirectoryName(modelPaths.ModelPath);

            return(Path.Combine(folderPath, dataFileName));
        }
Exemplo n.º 2
0
        public async Task <M <DataNodeName> > TryGetNodeAsync(ModelPaths modelPaths, Source source)
        {
            Log.Debug($"Get node for {source} in model {modelPaths}...");

            try
            {
                if (!TryGetParser(modelPaths, out IParser parser))
                {
                    return(Error.From($"File not supported: {modelPaths}"));
                }

                NodeDataSource nodeSource = new NodeDataSource(source.Text, source.LineNumber, source.Path);

                string nodeName = await parser.GetNodeAsync(modelPaths.ModelPath, nodeSource);

                if (nodeName == null)
                {
                    return(M.NoValue);
                }

                return((DataNodeName)nodeName);
            }
            catch (Exception e)
            {
                return(Error.From(e));
            }
        }
        public Task <M <IReadOnlyList <IDataItem> > > TryReadSaveAsync(ModelPaths modelPaths)
        {
            Log.Debug($"Try reading saved model layout: {modelPaths}");
            string saveFilePath = GetSaveFilePath(modelPaths);

            return(saveSerializer.DeserializeAsync(saveFilePath));
        }
        private static string GetCacheFilePath(ModelPaths modelPaths)
        {
            var    dataFileName  = Path.GetFileNameWithoutExtension(modelPaths.ModelPath);
            string cacheFileName = $"{dataFileName}.cache.json";

            return(Path.Combine(modelPaths.WorkFolderPath, cacheFileName));
        }
Exemplo n.º 5
0
        public async Task <M> ParseAsync(ModelPaths modelPaths, Action <IDataItem> itemsCallback)
        {
            Log.Debug($"Parse {modelPaths} ...");
            Timing t = Timing.Start();

            if (!TryGetParser(modelPaths, out IParser parser))
            {
                return(Error.From($"File not supported: {modelPaths}"));
            }

            void NodeCallback(NodeData nodeData) => itemsCallback(ToDataItem(nodeData));
            void LinkCallback(LinkData linkData) => itemsCallback(ToDataItem(linkData));

            try
            {
                await parser.ParseAsync(modelPaths.ModelPath, NodeCallback, LinkCallback);

                t.Log($"Parsed {modelPaths}");
                return(M.Ok);
            }
            catch (Exception e)
            {
                return(Error.From(e));
            }
        }
Exemplo n.º 6
0
 public void StartMonitorDataChanges(ModelPaths modelPaths)
 {
     if (TryGetParser(modelPaths, out IParser parser))
     {
         parser.StartMonitorDataChanges(modelPaths.ModelPath);
     }
 }
        private async Task CacheItemsAsync(ModelPaths modelPaths, IReadOnlyList <IDataItem> items)
        {
            string cacheFilePath = GetCacheFilePath(modelPaths);

            IReadOnlyList <IDataItem> cacheItems = await GetCacheItemsAsync(items);

            await cacheSerializer.SerializeAsync(cacheItems, cacheFilePath);
        }
Exemplo n.º 8
0
        public DateTime GetDataTime(ModelPaths modelPaths)
        {
            if (!TryGetParser(modelPaths, out IParser parser))
            {
                return(DateTime.MinValue);
            }

            return(parser.GetDataTime(modelPaths.ModelPath));
        }
Exemplo n.º 9
0
        private bool TryGetParser(ModelPaths modelPaths, out IParser parser)
        {
            parser = parsers.FirstOrDefault(p => p.CanSupport(modelPaths.ModelPath));
            if (parser == null)
            {
                Log.Warn($"No supported parser for {modelPaths}");
            }

            return(parser != null);
        }
        public DateTime GetSaveTime(ModelPaths modelPaths)
        {
            string saveFilePath = GetSaveFilePath(modelPaths);

            if (!File.Exists(saveFilePath))
            {
                return(DateTime.MinValue);
            }

            return(File.GetLastWriteTime(saveFilePath));
        }
        public async Task OpenModelAsync(ModelPaths modelPaths)
        {
            string solutionFilePath = modelPaths.ModelPath;

            string serverName = ApiServerNames.ServerName <IVsExtensionApi>(solutionFilePath);

            Log.Debug($"Calling: {serverName}");
            bool      isStartedDependinator = false;
            Stopwatch t = Stopwatch.StartNew();

            while (t.Elapsed < TimeSpan.FromSeconds(60))
            {
                try
                {
                    if (ApiIpcClient.IsServerRegistered(serverName))
                    {
                        Log.Debug($"Server started after {t.Elapsed}: {serverName}");
                        if (!isStartedDependinator)
                        {
                            using (ApiIpcClient apiIpcClient = new ApiIpcClient(serverName))
                            {
                                apiIpcClient.Service <IVsExtensionApi>().Activate();
                            }
                        }

                        return;
                    }

                    // IVsExtensionApi not yet registered, lets try to start Dependinator, or wait a little.
                    if (!isStartedDependinator)
                    {
                        if (!await TryStartVisualStudioAsync(solutionFilePath))
                        {
                            return;
                        }

                        isStartedDependinator = true;
                        t.Restart();
                        await Task.Delay(1000);
                    }
                    else
                    {
                        await Task.Delay(500);
                    }
                }
                catch (Exception e)
                {
                    Log.Error($"Failed to check studio is running {e}");
                }
            }

            Log.Error("Failed to wait for other Dependiator instance");
        }
Exemplo n.º 12
0
        public async Task <M> TryReadCacheAsync(ModelPaths modelPaths, Action <IDataItem> dataItemsCallback)
        {
            parserService.StartMonitorDataChanges(modelPaths);

            M result = await persistenceService.TryReadCacheAsync(modelPaths, dataItemsCallback);

            if (result.IsFaulted)
            {
                return(result);
            }

            return(M.Ok);
        }
        public async Task <M> TryReadCacheAsync(ModelPaths modelPaths, Action <IDataItem> dataItemsCallback)
        {
            Log.Debug($"Try reading cached model: {modelPaths}");
            if (IsCacheOlderThanSave(modelPaths))
            {
                Log.Debug("Cache is older than saved layout data, ignoring cache.");
                return(M.NoValue);
            }

            string cacheFilePath = GetCacheFilePath(modelPaths);

            return(await cacheSerializer.TryDeserializeAsync(cacheFilePath, dataItemsCallback));
        }
        public CodeViewModel(
            ISolutionService solutionService,
            IProgressService progressService,
            ModelPaths modelPaths,
            string title,
            Window owner)
        {
            this.solutionService = solutionService;
            this.progressService = progressService;
            this.modelPaths      = modelPaths;
            this.owner           = owner;

            Title = title;
        }
        private bool IsCacheOlderThanSave(ModelPaths modelPaths)
        {
            DateTime saveTime  = GetSaveTime(modelPaths);
            DateTime cacheTime = GetCacheTime(modelPaths);

            Log.Debug($"Save time: {saveTime}, CacheTime time: {cacheTime}");

            if (saveTime == DateTime.MinValue || cacheTime == DateTime.MinValue)
            {
                return(false);
            }

            return(cacheTime < saveTime);
        }
        private bool IsSaveNewerThanData(ModelPaths modelPaths)
        {
            DateTime saveTime = GetSaveTime(modelPaths);
            DateTime dataTime = parserService.GetDataTime(modelPaths);

            Log.Debug($"Save time: {saveTime}, Data time: {dataTime}");

            if (saveTime == DateTime.MinValue || dataTime == DateTime.MinValue)
            {
                return(false);
            }

            return(saveTime > dataTime);
        }
Exemplo n.º 17
0
        public void TriggerDataChangedIfDataNewerThanCache(ModelPaths modelPaths)
        {
            DateTime cacheTime = persistenceService.GetCacheTime(modelPaths);
            DateTime dataTime  = parserService.GetDataTime(modelPaths);

            Log.Debug($"Data time: {dataTime}, cache time: {cacheTime}");

            if (dataTime > cacheTime)
            {
                Log.Debug("Data is newer than cache");
                Task.Delay(TimeSpan.FromSeconds(5))
                .ContinueWith(_ => DataChanged?.Invoke(this, EventArgs.Empty)).RunInBackground();
            }
        }
        public async Task SaveAsync(ModelPaths modelPaths, IReadOnlyList <IDataItem> items)
        {
            Log.Debug($"Saving model layout: {modelPaths}");
            Timing t = Timing.Start();

            await SaveItemsAsync(modelPaths, items);

            t.Log("Save items");

            Log.Debug($"Saving cache layout: {modelPaths}");
            await CacheItemsAsync(modelPaths, items);

            t.Log($"Cache {items.Count} items");
        }
        private static bool TryOpenInVisualStudio(ModelPaths modelPaths, Source source)
        {
            string solutionPath = modelPaths.ModelPath;
            string serverName   = ApiServerNames.ServerName <IVsExtensionApi>(solutionPath);

            if (!ApiIpcClient.IsServerRegistered(serverName))
            {
                return(false);
            }

            using (ApiIpcClient apiIpcClient = new ApiIpcClient(serverName))
            {
                apiIpcClient.Service <IVsExtensionApi>().ShowFile(source.Path, source.LineNumber);
                apiIpcClient.Service <IVsExtensionApi>().Activate();
            }

            return(true);
        }
        public async Task ShowCodeAsync(NodeName nodeName)
        {
            Source     source     = null;
            ModelPaths modelPaths = modelMetadata.ModelPaths;
            M <Source> result;

            using (progressService.ShowDialog("Getting source code.."))
            {
                result = await dataService.TryGetSourceAsync(modelPaths, nodeName);

                if (result.HasValue(out source))
                {
                    if (source.Path != null && File.Exists(source.Path))
                    {
                        if (TryOpenInVisualStudio(modelPaths, source))
                        {
                            return;
                        }

                        // Lets show the file in "our" code viewer
                        string fileText = File.ReadAllText(source.Path);
                        source = new Source(source.Path, fileText, source.LineNumber);
                    }
                }
            }

            if (result.IsFaulted)
            {
                if (result.Error == M.NoValue)
                {
                    message.ShowInfo($"No source available for {nodeName}");
                }
                else
                {
                    message.ShowWarning($"Error while showing code for:\n{nodeName}\n\n{result.ErrorMessage}");
                }
                return;
            }

            CodeDialog codeDialog = codeDialogProvider(
                nodeName, source, () => dataService.TryGetSourceAsync(modelPaths, nodeName));

            codeDialog.Show();
        }
        public async Task OpenFileAsync(ModelPaths modelPaths, string filePath, int lineNumber)
        {
            string solutionFilePath = modelPaths.ModelPath;

            string serverName = ApiServerNames.ServerName <IVsExtensionApi>(solutionFilePath);

            if (!ApiIpcClient.IsServerRegistered(serverName))
            {
                await OpenModelAsync(modelPaths);
            }

            if (ApiIpcClient.IsServerRegistered(serverName))
            {
                using (ApiIpcClient apiIpcClient = new ApiIpcClient(serverName))
                {
                    apiIpcClient.Service <IVsExtensionApi>().ShowFile(filePath, lineNumber);
                    apiIpcClient.Service <IVsExtensionApi>().Activate();
                }
            }
        }
        private async Task SaveItemsAsync(ModelPaths modelPaths, IReadOnlyList <IDataItem> items)
        {
            Timing t = Timing.Start();
            IReadOnlyList <IDataItem> saveItems = await GetSaveItemsAsync(items);

            t.Log("Got items");

            string saveFilePath = GetSaveFilePath(modelPaths);

            if (IsSaveNewerThanData(modelPaths))
            {
                await saveSerializer.SerializeMergedAsync(saveItems, saveFilePath);

                t.Log("saved merged");
            }
            else
            {
                await saveSerializer.SerializeAsync(saveItems, saveFilePath);

                t.Log("saved full");
            }
        }
Exemplo n.º 23
0
        public async Task <M <Source> > GetSourceAsync(ModelPaths modelPaths, DataNodeName nodeName)
        {
            Log.Debug($"Get source for {nodeName} in model {modelPaths}...");
            try
            {
                if (!TryGetParser(modelPaths, out IParser parser))
                {
                    return(Error.From($"File not supported: {modelPaths}"));
                }

                NodeDataSource source = await parser.GetSourceAsync(modelPaths.ModelPath, (string)nodeName);

                if (source == null)
                {
                    return(M.NoValue);
                }

                return(new Source(source.Path, source.Text, source.LineNumber));
            }
            catch (Exception e)
            {
                return(Error.From(e));
            }
        }
Exemplo n.º 24
0
 public Task OpenModelAsync(ModelPaths modelPaths) => solutionService.OpenModelAsync(modelPaths);
Exemplo n.º 25
0
 public async Task <M <DataNodeName> > TryGetNodeAsync(ModelPaths modelPaths, Source source) =>
 await parserService.TryGetNodeAsync(modelPaths, source);
Exemplo n.º 26
0
 public async Task <M <Source> > TryGetSourceAsync(ModelPaths modelPaths, DataNodeName nodeName) =>
 await parserService.GetSourceAsync(modelPaths, nodeName);
Exemplo n.º 27
0
 public Task SaveAsync(ModelPaths modelPaths, IReadOnlyList <IDataItem> items)
 {
     return(persistenceService.SaveAsync(modelPaths, items));
 }
Exemplo n.º 28
0
 public Task <M> TryReadFreshAsync(ModelPaths modelPaths, Action <IDataItem> dataItemsCallback)
 {
     parserService.StartMonitorDataChanges(modelPaths);
     return(parserService.ParseAsync(modelPaths, dataItemsCallback));
 }
Exemplo n.º 29
0
 public Task <M <IReadOnlyList <IDataItem> > > TryReadSaveAsync(ModelPaths modelPaths)
 {
     parserService.StartMonitorDataChanges(modelPaths);
     return(persistenceService.TryReadSaveAsync(modelPaths));
 }