public async Task ExtractClass_with_filename_set_copies_element_to_new_file_and_inserts_ux_class_attribute()
        {
            // Setup
            _root = CreateRootElement();
            var      projectClasses  = new BehaviorSubject <IEnumerable <IElement> >(new IElement[] {});
            var      project         = Substitute.For <IProject>();
            IElement createdFileRoot = null;

            project.Classes.ReturnsForAnyArgs(projectClasses);

            project
            .CreateDocument(default(RelativeFilePath))
            .ReturnsForAnyArgs(
                ci =>
            {
                var fragment           = ci.Arg <SourceFragment>();
                var nativeRelativePath = ci.Arg <RelativeFilePath>().NativeRelativePath;
                createdFileRoot        = CreateRootElement(fragment, nativeRelativePath);
                projectClasses.OnNext(new[] { createdFileRoot });
                return(Task.FromResult(false));                                // no idea what i'm doing
            });
            var classExtractor = new ClassExtractor(project);

            // Do
            await classExtractor.ExtractClass(await GetTreeChild("Panel"), "MyPanel", RelativeFilePath.Parse("CustomPanel.ux"));

            // Assert
            Assert.That(createdFileRoot, Is.Not.Null);
            Assert.That(await createdFileRoot.Name.FirstAsync(), Is.EqualTo("Panel"));
            Assert.That(await createdFileRoot.UxClass().FirstAsync(), Is.EqualTo(Optional.Some("MyPanel")));
            Assert.That(await GetTreeChild("MyPanel"), Is.Not.Null);
        }
        public async Task ExtractClass_turns_element_into_a_class_and_inserts_an_instance_of_that_class_beside_it()
        {
            _root = CreateRootElement();
            var classExtractor = new ClassExtractor(Substitute.For <IProject>());
            await classExtractor.ExtractClass(await GetTreeChild("Panel"), "CustomPanel", Optional.None());

            var uxClassAttribute = await(await GetTreeChild("Panel")).UxClass().FirstAsync();

            Assert.That(uxClassAttribute, Is.EqualTo(Optional.Some("CustomPanel")));
            Assert.That(await GetTreeChild("CustomPanel"), Is.Not.Null);
        }
        public async Task ExtractClass_prints_message_to_logview_when_exception_is_thrown()
        {
            _root = CreateRootElement();
            var projectClasses = new BehaviorSubject <IEnumerable <IElement> >(new IElement[] { });
            var project        = Substitute.For <IProject>();

            project.Classes.ReturnsForAnyArgs(projectClasses);

            project
            .CreateDocument(default(RelativeFilePath))
            .ReturnsForAnyArgs(_ =>
                               Task.Run(() => { throw new IOException("Couldn't write a file, as the disk just imploded"); }));

            var    classExtractor = new ClassExtractor(project);
            string logMessage     = null;

            using (classExtractor.LogMessages.Subscribe(message => logMessage = message))
            {
                await classExtractor.ExtractClass(await GetTreeChild("Panel"), "MyPanel", RelativeFilePath.Parse("CustomPanel.ux"));
            }

            Assert.That(logMessage, Is.EqualTo("Error: Unable to create class. Couldn't write a file, as the disk just imploded\r\n"));
        }
Beispiel #4
0
        public Window Execute(IDocument document, string[] args)
        {
            var hack    = new ReplaySubject <IObservable <BytecodeGenerated> >(1);
            var project = new LiveProject(document, _shell, hack.Switch().ObserveOn(Application.MainThread));
            var ignore  = RecentProjects.Bump(document, project.Name.ObserveOn(Application.MainThread));

            var preview = _previewService.StartPreview(project);

            hack.OnNext(preview.Bytecode);

                        #pragma warning disable 0219
            var garbage = ExternalSelection.UpdateProjectContext(_daemon, project);
                        #pragma warning restore 0219

            var inspector = Fuse.Inspector.Inspector.Create(project);

            var buildArgs = new BuildArgs(args);

            var usbMode         = new USBMode(new AndroidPortReverser(), Observable.Return(preview.Port), _previewService);
            var previewOnDevice = new PreviewOnDevice(_fuse, project, buildArgs);

            var build  = new Build(project, preview, previewOnDevice, usbMode.EnableUsbModeCommand, buildArgs);
            var export = new Export(project, _fuse, buildArgs);

            var projHost = new ProjectHost(build.BuildArguments, preview, project, self =>
                                           _projectsOpen.OnNext(_projectsOpen.Value.Remove(self)));
            _projectsOpen.OnNext(_projectsOpen.Value.Add(projHost));

            var codeView = new CodeView(preview.AccessCode, NetworkHelper
                                        .GetInterNetworkIps()
                                        .ToArray());

            // Viewport stuff

            var selectionEnabled = Property.Create(false);


            var glVersion = new Subject <OpenGlVersion>();

            var viewport = new ViewportFactory(
                preview,
                selectionEnabled,
                preview.Port,
                project,
                _fuse,
                glVersion);

            var mode           = UserSettings.Enum <Mode>("WindowMode").Or(Mode.Normal);
            var previewDevices = new PreviewDevices(project, _shell);
            var workspace      = new StageController(
                viewport,
                previewDevices,
                selectionEnabled);

            var topMost    = Property.Create(false);
            var windowMenu =
                Menu.Toggle(
                    name: "Compact mode",
                    toggle: mode.Convert(
                        convert: m => m == Mode.Compact,
                        convertBack: b => b ? Mode.Compact : Mode.Normal),
                    hotkey: HotKey.Create(ModifierKeys.Meta, Key.M))
                + Menu.Separator
                + Menu.Toggle(
                    name: "Keep window on top",
                    toggle: topMost,
                    hotkey: HotKey.Create((_fuse.Platform == OS.Windows ? ModifierKeys.Shift : ModifierKeys.Alt) | ModifierKeys.Meta, Key.T))
                + Menu.Separator
                + Menu.Option(
                    value: Themes.OriginalLight,
                    name: "Light theme",
                    property: Theme.CurrentTheme)
                + Menu.Option(
                    value: Themes.OriginalDark,
                    name: "Dark theme",
                    property: Theme.CurrentTheme);

            var messages = preview.Messages.Replay(TimeSpan.FromSeconds(2)).RefCount();

            project.FilePath.SubscribeUsing(projPath =>
                                            PushEventsToDaemon.Start(
                                                messages: messages,
                                                daemon: _daemon,
                                                projectPath: projPath,
                                                projectId: ProjectIdComputer.IdFor(projPath),
                                                target: BuildTarget.DotNetDll));

            var sketchConverter = new SketchWatcher(_fuse.Report, _shell);

            var classExtractor = new ClassExtractor(project);

            var buildStartedOrLicenseChanged = project.FilePath
                                               .CombineLatest(build.Rebuilt.StartWith(Unit.Default));

            var allLogMessages = messages.ToLogMessages()
                                 .Merge(_setupGuide.LogMessages)
                                 .Merge(export.LogMessages)
                                 .Merge(project.LogMessages)
                                 .Merge(previewOnDevice.LogMessages)
                                 .Merge(classExtractor.LogMessages)
                                 .Merge(_hostRequestMessages)
                                 .Merge(_errors.Select(e => e.Message))
                                 .Merge(AutoReload.Log.Select(msg => "[Fusion AutoReload]: " + msg)
                                        .Merge(glVersion.Take(1).ToLogMessages(_fuse.Report)))
                                 .Merge(preview.LogMessages)
                                 .Merge(_previewService.LogMessages)
                                 .Merge(sketchConverter.LogMessages)
                                 .Merge(previewDevices.LogMessages);

            var stopPreview = preview.Start(build.BuildArguments);

            // start with a viewport
            workspace.NewViewport.ExecuteOnce();

            var projectMenu =
                ProjectMenu.CommandItems(project.FilePath.Select(Optional.Some), _shell)
                + Menu.Separator
                + ProjectMenu.FileItems(project, _shell)
                + Menu.Separator
                + Menu.Item("Sketch import", sketchConverter.ShowDialog());

            var help = new Help();

            var debug = new Debug(project);


            var elementContext = new ElementContext(project.Context, project, _daemon);

            var errorView = new ErrorView(messages, project.FilePath, _daemon, preview.ClientRemoved);

            var logView     = new LogView(allLogMessages, messages, errorView);
            var sketchWatch = sketchConverter.Watch(project);
            var window      = MainWindow.Create(
                projectName: project.Name,
                search: Control.Empty,                //search.Control,
                outline: CreateLeftPane(project, elementContext, project.Context, _shell, classExtractor),
                bookmarks: Control.Empty,
                stage: workspace,
                notifications: Layout.StackFromTop(
                    SimulatorNotifications.CreateBusyIndicator(messages),
                    SimulatorNotifications.Create(messages, build.Rebuild, logView.IsExpanded)),
                inspector: inspector,
                attributes: Control.Empty,                //DataContext.Create(workspace.Viewport, selection.Element),
                logview: logView,
                menu: MainMenu.Create(
                    _fuse,
                    _shell,
                    workspace,
                    help,
                    elementContext.CreateMenu(project.Context.CurrentSelection),
                    projectMenu,
                    build,
                    export,
                    _setupGuide,
                    windowMenu,
                    debug),
                closed: Command.Enabled(() =>
            {
                stopPreview.Dispose();
                preview.Dispose();
                projHost.Dispose();
                project.FilePath.Take(1).Subscribe(path =>
                                                   _daemon.Broadcast(new ProjectClosed {
                    ProjectId = ProjectIdComputer.IdFor(path)
                }));
                project.Dispose();
                workspace.Dispose();
                sketchWatch.Dispose();
            }),
                selectionEnabled: selectionEnabled,
                topMost: topMost,
                mode: mode,
                mainWindowFocused: _mainWindowFocused.Update(Unit.Default),
                context: project.Context,
                codeView: codeView);

            return(window);
        }