private void AddCakeFile(string cakeFilePath)
        {
            try
            {
                var cakeScript = _scriptService.Generate(new FileChange
                {
                    FileName = cakeFilePath,
                    FromDisk = true
                });

                var project = GetProject(cakeScript, cakeFilePath);

                // add Cake project to workspace
                _workspace.AddProject(project);
                var documentId   = DocumentId.CreateNewId(project.Id);
                var loader       = TextLoader.From(TextAndVersion.Create(SourceText.From(cakeScript.Source), VersionStamp.Create(DateTime.UtcNow)));
                var documentInfo = DocumentInfo.Create(
                    documentId,
                    cakeFilePath,
                    filePath: cakeFilePath,
                    loader: loader,
                    sourceCodeKind: SourceCodeKind.Script);

                _workspace.AddDocument(documentInfo);
                _projects[cakeFilePath] = project;
                _logger.LogInformation($"Added Cake project '{cakeFilePath}' to the workspace.");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{cakeFilePath} will be ignored due to an following error");
            }
        }
        public async Task <object> Handle(ChangeBufferRequest request)
        {
            if (request.FileName == null)
            {
                return(true);
            }

            var script = _scriptService.Generate(new FileChange
            {
                FileName    = request.FileName,
                LineChanges = { new LineChange
                                {
                                    StartLine   = request.StartLine,
                                    StartColumn = request.StartColumn,
                                    EndLine     = request.EndLine,
                                    EndColumn   = request.EndColumn,
                                    NewText     = request.NewText
                                } }
            });

            // Redirect to UpdateBuffer
            await _workspace.BufferManager.UpdateBufferAsync(new UpdateBufferRequest
            {
                Buffer   = script.Source,
                FileName = request.FileName
            });

            return(true);
        }
Exemple #3
0
        public void Initalize(IConfiguration configuration)
        {
            _logger.LogInformation($"Detecting Cake files in '{_environment.TargetDirectory}'.");

            // Nothing to do if there are no Cake files
            var allCakeFiles = Directory.GetFiles(_environment.TargetDirectory, "*.cake", SearchOption.AllDirectories);

            if (allCakeFiles.Length == 0)
            {
                _logger.LogInformation("Could not find any Cake files");
                return;
            }

            _logger.LogInformation($"Found {allCakeFiles.Length} Cake files.");

            // Check that script service is connected
            if (!_scriptService.IsConnected)
            {
                _logger.LogWarning("Cake script service not connected. Aborting.");
                return;
            }

            foreach (var cakePath in allCakeFiles)
            {
                try
                {
                    var cakeScript = _scriptService.Generate(new FileChange
                    {
                        FileName = cakePath,
                        FromDisk = true
                    });
                    var project = GetProject(cakeScript, cakePath);

                    // add Cake project to workspace
                    _workspace.AddProject(project);
                    var documentId   = DocumentId.CreateNewId(project.Id);
                    var loader       = new CakeTextLoader(cakePath, _scriptService);
                    var documentInfo = DocumentInfo.Create(
                        documentId,
                        cakePath,
                        filePath: cakePath,
                        loader: loader,
                        sourceCodeKind: SourceCodeKind.Script);

                    _workspace.AddDocument(documentInfo);
                    _projects[cakePath] = project;
                    _logger.LogInformation($"Added Cake project '{cakePath}' to the workspace.");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"{cakePath} will be ignored due to an following error");
                }
            }

            // Hook up Cake script events
            _scriptService.ReferencesChanged += ScriptReferencesChanged;
            _scriptService.UsingsChanged     += ScriptUsingsChanged;
        }
Exemple #4
0
        public async Task <object> Handle(UpdateBufferRequest request)
        {
            if (request.FileName == null)
            {
                return(true);
            }

            var fileChange = new FileChange
            {
                FileName = request.FileName
            };

            if (request.Changes != null)
            {
                foreach (var change in request.Changes)
                {
                    fileChange.LineChanges.Add(new LineChange
                    {
                        StartLine   = change.StartLine,
                        StartColumn = change.StartColumn,
                        EndLine     = change.EndLine,
                        EndColumn   = change.EndColumn,
                        NewText     = change.NewText
                    });
                }

                fileChange.FromDisk = false;
            }
            else
            {
                fileChange.Buffer   = request.Buffer;
                fileChange.FromDisk = request.FromDisk;
            }
            var script = _scriptService.Generate(fileChange);

            // Avoid having buffer manager reading from disk
            request.FromDisk = false;
            request.Buffer   = script.Source;
            request.Changes  = null;

            await _workspace.BufferManager.UpdateBufferAsync(request);

            return(true);
        }
Exemple #5
0
        public override Task <TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
        {
            var prevLastWriteTime = File.GetLastWriteTimeUtc(_filePath);

            var script = _scriptService.Generate(new FileChange
            {
                FileName = _filePath,
                FromDisk = true
            });
            var version        = VersionStamp.Create(prevLastWriteTime);
            var text           = SourceText.From(script.Source);
            var textAndVersion = TextAndVersion.Create(text, version, _filePath);

            var newLastWriteTime = File.GetLastWriteTimeUtc(_filePath);

            if (!newLastWriteTime.Equals(prevLastWriteTime))
            {
                throw new IOException($"File was externally modified: {_filePath}");
            }

            return(Task.FromResult(textAndVersion));
        }