From 7ae75e6cb2be0b617f9183140903c12db9cf2b7e Mon Sep 17 00:00:00 2001 From: Ahmed ElSayed Date: Thu, 25 May 2017 11:29:09 -0700 Subject: [PATCH] Allow manually provided function.json file. Closes #50 --- .../MakeFunction/BuildArtifactsLog.cs | 74 +++++++++++++++++++ .../MakeFunction/FunctionJsonConverter.cs | 16 +++- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.NET.Sdk.Functions/MakeFunction/BuildArtifactsLog.cs diff --git a/src/Microsoft.NET.Sdk.Functions/MakeFunction/BuildArtifactsLog.cs b/src/Microsoft.NET.Sdk.Functions/MakeFunction/BuildArtifactsLog.cs new file mode 100644 index 0000000..69c698b --- /dev/null +++ b/src/Microsoft.NET.Sdk.Functions/MakeFunction/BuildArtifactsLog.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace MakeFunctionJson +{ + internal class BuildArtifactsLog + { + private const string buildArtifactsLogName = "functionsSdk.out"; + private readonly HashSet _artifacts; + private readonly FakeLogger _logger; + private readonly string _logPath; + + public BuildArtifactsLog(string outputPath, FakeLogger logger) + { + _logger = logger; + _artifacts = new HashSet(StringComparer.OrdinalIgnoreCase); + _logPath = Path.Combine(outputPath, buildArtifactsLogName); + try + { + if (File.Exists(_logPath)) + { + foreach (var line in File.ReadAllLines(_logPath)) + { + _artifacts.Add(line); + } + } + } + catch (Exception e) + { + _logger.LogWarning($"Unable to read file {_logPath}"); + _logger.LogWarningFromException(e); + } + } + + public bool TryClearBuildArtifactsLog() + { + return Try(() => File.Delete(_logPath), $"Unable to clean file '{_logPath}'", true); + } + + public bool TryAddBuildArtifact(string name) + { + return Try(() => File.AppendAllLines(_logPath, new[] { name }), $"Unable to update file '{_logPath}'", true); + } + + public bool IsBuildArtifact(string name) + { + return _artifacts.Contains(name); + } + + private bool Try(Action action, string message, bool isError) + { + try + { + action(); + return true; + } + catch (Exception e) + { + if (isError) + { + _logger.LogError(message); + _logger.LogErrorFromException(e); + } + else + { + _logger.LogWarning(message); + _logger.LogWarningFromException(e); + } + return false; + } + } + } +} diff --git a/src/Microsoft.NET.Sdk.Functions/MakeFunction/FunctionJsonConverter.cs b/src/Microsoft.NET.Sdk.Functions/MakeFunction/FunctionJsonConverter.cs index 1e5e6fb..3e1bf91 100644 --- a/src/Microsoft.NET.Sdk.Functions/MakeFunction/FunctionJsonConverter.cs +++ b/src/Microsoft.NET.Sdk.Functions/MakeFunction/FunctionJsonConverter.cs @@ -14,6 +14,7 @@ internal class FunctionJsonConverter private string _outputPath; private readonly FakeLogger _log; private readonly IDictionary _functionNamesSet; + private readonly BuildArtifactsLog _buildArtifactsLog; internal FunctionJsonConverter(string assemblyPath, string outputPath, TaskLoggingHelper log) { @@ -35,6 +36,7 @@ internal FunctionJsonConverter(string assemblyPath, string outputPath, TaskLoggi } _log = new FakeLogger(log); _functionNamesSet = new Dictionary(StringComparer.OrdinalIgnoreCase); + _buildArtifactsLog = new BuildArtifactsLog(_outputPath, _log); } /// @@ -54,6 +56,12 @@ internal bool TryRun() try { this._functionNamesSet.Clear(); + if (!_buildArtifactsLog.TryClearBuildArtifactsLog()) + { + _log.LogError("Unable to clean build artifacts file."); + return false; + } + CleanOutputPath(); #if NET46 var assembly = Assembly.LoadFrom(_assemblyPath); @@ -69,8 +77,11 @@ internal bool TryRun() { var functionJson = method.ToFunctionJson(relativeAssemblyPath); var functionName = method.GetSdkFunctionName(); - var path = Path.Combine(_outputPath, functionName, "function.json"); - if (CheckAppSettingsAndFunctionName(functionJson, method)) + var artifactName = Path.Combine(functionName, "function.json"); + var path = Path.Combine(_outputPath, artifactName); + if (!File.Exists(path) && + CheckAppSettingsAndFunctionName(functionJson, method) && + _buildArtifactsLog.TryAddBuildArtifact(artifactName)) { functionJson.Serialize(path); } @@ -171,6 +182,7 @@ private void CleanOutputPath() Directory.GetDirectories(_outputPath) .Select(d => Path.Combine(d, "function.json")) .Where(File.Exists) + .Where(f => _buildArtifactsLog.IsBuildArtifact(f.Replace(_outputPath, string.Empty))) .Select(Path.GetDirectoryName) .ToList() .ForEach(directory =>