Skip to content
This repository was archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Add directories to current cache context
Browse files Browse the repository at this point in the history
- Flow the ICacheContextAccessor through the DI system
- Watch all directories that source files are currently in (including)
 the project directory.

#552
  • Loading branch information
davidfowl committed Aug 20, 2014
1 parent 28dc7a0 commit b8a96d1
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 26 deletions.
8 changes: 6 additions & 2 deletions src/Microsoft.Framework.DesignTimeHost/ApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class ApplicationContext
{
private readonly IServiceProvider _hostServices;
private readonly ICache _cache;
private readonly ICacheContextAccessor _cacheContextAccessor;

private readonly Queue<Message> _inbox = new Queue<Message>();
private readonly object _processingLock = new object();

Expand All @@ -42,10 +44,11 @@ public class ApplicationContext
private readonly List<CompiledAssemblyState> _waitingForCompiledAssemblies = new List<CompiledAssemblyState>();
private readonly List<ConnectionContext> _waitingForDiagnostics = new List<ConnectionContext>();

public ApplicationContext(IServiceProvider services, ICache cache, int id)
public ApplicationContext(IServiceProvider services, ICache cache, ICacheContextAccessor cacheContextAccessor, int id)
{
_hostServices = services;
_cache = cache;
_cacheContextAccessor = cacheContextAccessor;
Id = id;
}

Expand Down Expand Up @@ -501,7 +504,8 @@ private State Initialize(string appPath, FrameworkName targetFramework, string c
packagesDirectory: null,
configuration: configuration,
targetFramework: targetFramework,
cache: _cache);
cache: _cache,
cacheContextAccessor: _cacheContextAccessor);

Project project = applicationHostContext.Project;

Expand Down
9 changes: 8 additions & 1 deletion src/Microsoft.Framework.DesignTimeHost/ConnectionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ public class ConnectionContext
private readonly IDictionary<int, ApplicationContext> _contexts;
private readonly IServiceProvider _services;
private readonly ICache _cache;
private readonly ICacheContextAccessor _cacheContextAccessor;
private ProcessingQueue _queue;
private string _hostId;

public ConnectionContext(IDictionary<int, ApplicationContext> contexts,
IServiceProvider services,
ICache cache,
ICacheContextAccessor cacheContextAccessor,
ProcessingQueue queue,
string hostId)
{
_contexts = contexts;
_services = services;
_cache = cache;
_cacheContextAccessor = cacheContextAccessor;
_queue = queue;
_hostId = hostId;
}
Expand All @@ -53,7 +56,11 @@ public void OnReceive(Message message)
{
Trace.TraceInformation("[ConnectionContext]: Creating new application context for {0}", message.ContextId);

applicationContext = new ApplicationContext(_services, _cache, message.ContextId);
applicationContext = new ApplicationContext(_services,
_cache,
_cacheContextAccessor,
message.ContextId);

_contexts.Add(message.ContextId, applicationContext);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Framework.DesignTimeHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void Main(string[] args)

private async Task OpenChannel(int port, string hostId)
{
var cache = new Cache();
var cacheContextAccessor = new CacheContextAccessor();
var cache = new Cache(cacheContextAccessor);
var contexts = new Dictionary<int, ApplicationContext>();

// This fixes the mono incompatibility but ties it to ipv4 connections
Expand All @@ -65,7 +66,7 @@ private async Task OpenChannel(int port, string hostId)

var stream = new NetworkStream(acceptSocket);
var queue = new ProcessingQueue(stream);
var connection = new ConnectionContext(contexts, _services, cache, queue, hostId);
var connection = new ConnectionContext(contexts, _services, cache, cacheContextAccessor, queue, hostId);

queue.OnReceive += message =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BuildContext
private readonly string _outputPath;
private readonly ApplicationHostContext _applicationHostContext;

public BuildContext(ICache cache, Project project, FrameworkName targetFramework, string configuration, string outputPath)
public BuildContext(ICache cache, ICacheContextAccessor cacheContextAccessor, Project project, FrameworkName targetFramework, string configuration, string outputPath)
{
_project = project;
_targetFramework = targetFramework;
Expand All @@ -30,7 +30,8 @@ public BuildContext(ICache cache, Project project, FrameworkName targetFramework
packagesDirectory: null,
configuration: configuration,
targetFramework: targetFramework,
cache: cache);
cache: cache,
cacheContextAccessor: cacheContextAccessor);
}

public void Initialize()
Expand Down
10 changes: 8 additions & 2 deletions src/Microsoft.Framework.PackageManager/Building/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public bool Build()

host.Initialize();

var cache = new Cache();
var cacheContextAccessor = new CacheContextAccessor();
var cache = new Cache(cacheContextAccessor);

using (host.AddLoaders(loaderContainer))
{
Expand All @@ -118,7 +119,12 @@ public bool Build()
var errors = new List<string>();
var warnings = new List<string>();

var context = new BuildContext(cache, project, targetFramework, configuration, baseOutputPath);
var context = new BuildContext(cache,
cacheContextAccessor,
project,
targetFramework,
configuration,
baseOutputPath);
context.Initialize();

if (_buildOptions.CheckDiagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public class DependencyContext
{
public DependencyContext(string projectDirectory, string configuration, FrameworkName targetFramework)
{
var cacheContextAccessor = new CacheContextAccessor();
var cache = new Cache(cacheContextAccessor);

var applicationHostContext = new ApplicationHostContext(
serviceProvider: null,
projectDirectory: projectDirectory,
packagesDirectory: null,
configuration: configuration,
targetFramework: targetFramework,
cache: new Cache());
cache: cache,
cacheContextAccessor: cacheContextAccessor);

ProjectResolver = applicationHostContext.ProjectResolver;
NuGetDependencyResolver = applicationHostContext.NuGetDependencyProvider;
Expand Down
29 changes: 21 additions & 8 deletions src/Microsoft.Framework.Runtime.Roslyn/RoslynCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ namespace Microsoft.Framework.Runtime.Roslyn
public class RoslynCompiler
{
private readonly ICache _cache;
private readonly ICacheContextAccessor _cacheContextAccessor;
private readonly IFileWatcher _watcher;

public RoslynCompiler(ICache cache, IFileWatcher watcher)
public RoslynCompiler(ICache cache,
ICacheContextAccessor cacheContextAccessor,
IFileWatcher watcher)
{
_cache = cache;
_cacheContextAccessor = cacheContextAccessor;
_watcher = watcher;
}

Expand All @@ -45,13 +49,6 @@ public CompilationContext CompileProject(
Trace.TraceInformation("[{0}]: Compiling '{1}'", GetType().Name, name);
var sw = Stopwatch.StartNew();

_watcher.WatchDirectory(path, ".cs");

foreach (var directory in Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories))
{
_watcher.WatchDirectory(directory, ".cs");
}

var compilationSettings = project.GetCompilationSettings(targetFramework, configuration);

IList<SyntaxTree> trees = GetSyntaxTrees(project, compilationSettings, incomingSourceReferences);
Expand Down Expand Up @@ -128,8 +125,13 @@ private IList<SyntaxTree> GetSyntaxTrees(Project project,
var parseOptions = new CSharpParseOptions(languageVersion: compilationSettings.LanguageVersion,
preprocessorSymbols: compilationSettings.Defines.AsImmutable());

var dirs = new HashSet<string>();
dirs.Add(project.ProjectDirectory);

foreach (var sourcePath in project.SourceFiles)
{
dirs.Add(Path.GetDirectoryName(sourcePath));

_watcher.WatchFile(sourcePath);

var syntaxTree = CreateSyntaxTree(sourcePath, parseOptions);
Expand All @@ -141,13 +143,24 @@ private IList<SyntaxTree> GetSyntaxTrees(Project project,
{
var sourcePath = sourceFileReference.Path;

dirs.Add(Path.GetDirectoryName(sourcePath));

_watcher.WatchFile(sourcePath);

var syntaxTree = CreateSyntaxTree(sourcePath, parseOptions);

trees.Add(syntaxTree);
}

// Watch all directories
var ctx = _cacheContextAccessor.Current;

foreach (var d in dirs)
{
ctx.Monitor(new FileWriteTimeCacheDependency(d));
_watcher.WatchDirectory(d, ".cs");
}

return trees;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class RoslynProjectReferenceProvider : IProjectReferenceProvider
{
private readonly RoslynCompiler _compiler;

public RoslynProjectReferenceProvider(ICache cache, IFileWatcher watcher)
public RoslynProjectReferenceProvider(ICache cache, ICacheContextAccessor cacheContextAccessor, IFileWatcher watcher)
{
_compiler = new RoslynCompiler(cache, watcher);
_compiler = new RoslynCompiler(cache, cacheContextAccessor, watcher);
}

public IMetadataProjectReference GetProjectReference(
Expand Down
4 changes: 3 additions & 1 deletion src/Microsoft.Framework.Runtime/ApplicationHostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public ApplicationHostContext(IServiceProvider serviceProvider,
string packagesDirectory,
string configuration,
FrameworkName targetFramework,
ICache cache)
ICache cache,
ICacheContextAccessor cacheContextAccessor)
{
ProjectDirectory = projectDirectory;
RootDirectory = Runtime.ProjectResolver.ResolveRootDirectory(ProjectDirectory);
Expand Down Expand Up @@ -59,6 +60,7 @@ public ApplicationHostContext(IServiceProvider serviceProvider,
_serviceProvider.Add(typeof(ProjectReferenceDependencyProvider), ProjectDepencyProvider);
_serviceProvider.Add(typeof(ILibraryManager), new LibraryManager(targetFramework, configuration, DependencyWalker, compositeDependencyExporter, cache));
_serviceProvider.Add(typeof(ICache), cache);
_serviceProvider.Add(typeof(ICacheContextAccessor), cacheContextAccessor);
}

public void AddService(Type type, object instance)
Expand Down
4 changes: 0 additions & 4 deletions src/Microsoft.Framework.Runtime/Caching/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public class Cache : ICache
private readonly ConcurrentDictionary<object, Lazy<CacheEntry>> _entries = new ConcurrentDictionary<object, Lazy<CacheEntry>>();
private readonly ICacheContextAccessor _accessor;

public Cache() : this(new CacheContextAccessor())
{
}

public Cache(ICacheContextAccessor accessor)
{
_accessor = accessor;
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.Framework.Runtime/DefaultHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ public void Dispose()

private void Initialize(DefaultHostOptions options, IServiceProvider hostServices)
{
var cacheContextAccessor = new CacheContextAccessor();
var cache = new Cache(cacheContextAccessor);

_applicationHostContext = new ApplicationHostContext(
hostServices,
_projectDirectory,
options.PackageDirectory,
options.Configuration,
_targetFramework,
new Cache());
cache,
cacheContextAccessor);

Trace.TraceInformation("[{0}]: Project path: {1}", GetType().Name, _projectDirectory);
Trace.TraceInformation("[{0}]: Project root: {1}", GetType().Name, _applicationHostContext.RootDirectory);
Expand Down

0 comments on commit b8a96d1

Please sign in to comment.