Skip to content

Commit

Permalink
Allow manually provided function.json file. Closes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmelsayed committed May 25, 2017
1 parent 68ef61c commit 7ae75e6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
74 changes: 74 additions & 0 deletions src/Microsoft.NET.Sdk.Functions/MakeFunction/BuildArtifactsLog.cs
Original file line number Diff line number Diff line change
@@ -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<string> _artifacts;
private readonly FakeLogger _logger;
private readonly string _logPath;

public BuildArtifactsLog(string outputPath, FakeLogger logger)
{
_logger = logger;
_artifacts = new HashSet<string>(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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class FunctionJsonConverter
private string _outputPath;
private readonly FakeLogger _log;
private readonly IDictionary<string, MethodInfo> _functionNamesSet;
private readonly BuildArtifactsLog _buildArtifactsLog;

internal FunctionJsonConverter(string assemblyPath, string outputPath, TaskLoggingHelper log)
{
Expand All @@ -35,6 +36,7 @@ internal FunctionJsonConverter(string assemblyPath, string outputPath, TaskLoggi
}
_log = new FakeLogger(log);
_functionNamesSet = new Dictionary<string, MethodInfo>(StringComparer.OrdinalIgnoreCase);
_buildArtifactsLog = new BuildArtifactsLog(_outputPath, _log);
}

/// <summary>
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 =>
Expand Down

0 comments on commit 7ae75e6

Please sign in to comment.