Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created May 16, 2012 01:05
Show Gist options
  • Select an option

  • Save anaisbetts/2706474 to your computer and use it in GitHub Desktop.

Select an option

Save anaisbetts/2706474 to your computer and use it in GitHub Desktop.
public class DropRepoViewModel : ReactiveObject, IDropRepoViewModel
{
public ReactiveAsyncCommand AnalyzeRepo { get; protected set; }
ObservableAsPropertyHelper<string> _CurrentRepoPath;
public string CurrentRepoPath { get { return _CurrentRepoPath.Value; } }
ObservableAsPropertyHelper<Dictionary<string, HeuristicTreeInformation>> _RepoAnalysis;
public Dictionary<string, HeuristicTreeInformation> RepoAnalysis { get { return _RepoAnalysis.Value; } }
ObservableAsPropertyHelper<Visibility> _RepairButtonVisibility;
public Visibility RepairButtonVisibility { get { return _RepairButtonVisibility.Value; } }
public ReactiveCommand RepairButton { get; protected set; }
public string UrlPathSegment {
get { return "drop"; }
}
public IScreen HostScreen { get; protected set; }
public DropRepoViewModel(IScreen hostScreen, IAppState appState,
[Named("AnalyzeFunc")][Optional] Func<object, IObservable<Tuple<string, Dictionary<string, HeuristicTreeInformation>>>> analyzeFunc = null)
{
HostScreen = hostScreen;
AnalyzeRepo = new ReactiveAsyncCommand();
var scanResult = AnalyzeRepo.RegisterAsyncObservable(analyzeFunc ?? (pathObj => {
Repository repo;
CoreUtility.ExtractLibGit2();
try {
repo = new Repository((string)pathObj);
} catch (Exception ex) {
UserError.Throw("This doesn't appear to be a Git repository", ex);
return Observable.Empty<Tuple<string, Dictionary<string, HeuristicTreeInformation>>>();
}
string path = (string) pathObj;
var scanAllBranches = repo.Branches.Select(branch =>
Observable.Defer(() =>
Observable.Start(() =>
branch.Tip.Tree.AnalyzeRepository(true), RxApp.TaskpoolScheduler)
.Select(x => new { Branch = branch.Name, Result = x })))
.Merge(2);
var scanWorkingDirectory = Observable.Defer(() => Observable.Start(() =>
new { Branch = "Working Directory", Result = TreeWalkerMixin.AnalyzeRepository(
allFilesInDirectory(path).Select(x => Tuple.Create(x, (Stream)File.OpenRead(x))), true) }));
return scanAllBranches.Merge(scanWorkingDirectory)
.Aggregate(new Dictionary<string, HeuristicTreeInformation>(),
(acc, x) => { acc[x.Branch] = x.Result; return acc; })
.Select(x => Tuple.Create(path, x));
}));
scanResult.Select(x => x.Item1).ToProperty(this, x => x.CurrentRepoPath);
scanResult.Select(x => x.Item2).ToProperty(this, x => x.RepoAnalysis);
this.WhenAny(x => x.RepoAnalysis, x => x.Value != null ? Visibility.Visible : Visibility.Hidden)
.ToProperty(this, x => x.RepairButtonVisibility);
RepairButton = new ReactiveCommand();
RepairButton.Subscribe(_ => {
appState.BranchInformation = RepoAnalysis.Keys.Where(x => x != "Working Directory").ToDictionary(x => x, x => RepoAnalysis[x]);
appState.WorkingDirectoryInformation = RepoAnalysis["Working Directory"];
appState.CurrentRepo = CurrentRepoPath;
HostScreen.Router.Navigate.Execute(RxApp.GetService<IRepairViewModel>());
});
}
IEnumerable<string> allFilesInDirectory(string rootPath)
{
var di = new DirectoryInfo(rootPath);
return di.GetDirectories().Where(x => x.Name != ".git")
.SelectMany(x => allFilesInDirectory(x.FullName))
.Concat(di.GetFiles().Select(x => x.FullName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment