Search in sources :

Example 1 with ArduinoLibraryInstaller

use of com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller in project tcMenu by davetcc.

the class MbedGeneratorTest method testMbedConversion.

@SuppressWarnings("unchecked")
@Test
public void testMbedConversion() throws IOException {
    ArduinoSketchFileAdjuster adjuster = Mockito.mock(ArduinoSketchFileAdjuster.class);
    MenuTree tree = buildTreeFromJson(LARGE_MENU_STRUCTURE);
    tree.addMenuItem(MenuTree.ROOT, new CustomBuilderMenuItemBuilder().withId(10001).withName("Authenticator").withMenuType(AUTHENTICATION).withEepromAddr(-1).menuItem());
    tree.addMenuItem(MenuTree.ROOT, new CustomBuilderMenuItemBuilder().withId(10002).withName("IoT Monitor").withMenuType(REMOTE_IOT_MONITOR).withEepromAddr(-1).menuItem());
    ArduinoLibraryInstaller installer = Mockito.mock(ArduinoLibraryInstaller.class);
    when(installer.statusOfAllLibraries()).thenReturn(new LibraryStatus(true, true, true, true));
    when(installer.getVersionOfLibrary("core-remote", InstallationType.CURRENT_PLUGIN)).thenReturn(VersionInfo.fromString("2.2.1"));
    var flashRemotes = List.of(new FlashRemoteId("name1", "first-uuid"), new FlashRemoteId("name2", "second-uuid"));
    var options = new CodeGeneratorOptionsBuilder().withPlatform(MBED_RTOS.getBoardId()).withAppName("tester").withNewId(SERVER_UUID).withEepromDefinition(new BspStm32EepromDefinition(50)).withAuthenticationDefinition(new ReadOnlyAuthenticatorDefinition("1234", flashRemotes)).withExpanderDefinitions(new IoExpanderDefinitionCollection()).withRecursiveNaming(true).withSaveToSrc(true).codeOptions();
    ArduinoGenerator generator = new ArduinoGenerator(adjuster, installer, MBED_RTOS);
    var firstPlugin = pluginConfig.getPlugins().get(0);
    firstPlugin.getProperties().stream().filter(p -> p.getName().equals("SWITCH_IODEVICE")).findFirst().ifPresent(p -> p.setLatestValue("io23017"));
    assertTrue(generator.startConversion(projectDir, pluginConfig.getPlugins(), tree, List.of(), options));
    var sourceDir = projectDir.resolve("src");
    var cppGenerated = new String(Files.readAllBytes(sourceDir.resolve(projectDir.getFileName() + "_menu.cpp")));
    var hGenerated = new String(Files.readAllBytes(sourceDir.resolve(projectDir.getFileName() + "_menu.h")));
    var pluginGeneratedH = new String(Files.readAllBytes(sourceDir.resolve("source.h")));
    var pluginGeneratedCPP = new String(Files.readAllBytes(sourceDir.resolve("source.cpp")));
    var pluginGeneratedTransport = new String(Files.readAllBytes(sourceDir.resolve("MySpecialTransport.h")));
    var cppTemplate = new String(Objects.requireNonNull(getClass().getResourceAsStream("/generator/templateMbed.cpp")).readAllBytes());
    var hTemplate = new String(Objects.requireNonNull(getClass().getResourceAsStream("/generator/templateMbed.h")).readAllBytes());
    cppGenerated = cppGenerated.replaceAll("#include \"tcmenu[^\"]*\"", "replacedInclude");
    cppTemplate = cppTemplate.replaceAll("#include \"tcmenu[^\"]*\"", "replacedInclude");
    // these files should line up. IF they do not because of the change in the ArduinoGenerator,
    // then make sure the change is good before adjusting the templates.
    assertEqualsIgnoringCRLF(cppTemplate, cppGenerated);
    assertEqualsIgnoringCRLF(hTemplate, hGenerated);
    assertEqualsIgnoringCRLF("CPP_FILE_CONTENT 10 otherKey", pluginGeneratedCPP);
    assertEqualsIgnoringCRLF("H_FILE_CONTENT 10 otherKey", pluginGeneratedH);
    assertEqualsIgnoringCRLF("My Transport file", pluginGeneratedTransport);
    Mockito.verify(adjuster).makeAdjustments(any(BiConsumer.class), eq(projectDir.resolve(sourceDir.resolve("project_main.cpp")).toString()), eq(projectDir.getFileName().toString()), anyCollection());
}
Also used : MenuTree(com.thecoderscorner.menu.domain.state.MenuTree) ArduinoGenerator(com.thecoderscorner.menu.editorui.generator.arduino.ArduinoGenerator) CodeGeneratorOptionsBuilder(com.thecoderscorner.menu.editorui.generator.CodeGeneratorOptionsBuilder) LibraryStatus(com.thecoderscorner.menu.editorui.generator.util.LibraryStatus) FlashRemoteId(com.thecoderscorner.menu.editorui.generator.parameters.auth.ReadOnlyAuthenticatorDefinition.FlashRemoteId) ArduinoSketchFileAdjuster(com.thecoderscorner.menu.editorui.generator.arduino.ArduinoSketchFileAdjuster) BspStm32EepromDefinition(com.thecoderscorner.menu.editorui.generator.parameters.eeprom.BspStm32EepromDefinition) ReadOnlyAuthenticatorDefinition(com.thecoderscorner.menu.editorui.generator.parameters.auth.ReadOnlyAuthenticatorDefinition) ArduinoLibraryInstaller(com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller) IoExpanderDefinitionCollection(com.thecoderscorner.menu.editorui.generator.parameters.IoExpanderDefinitionCollection) CustomBuilderMenuItemBuilder(com.thecoderscorner.menu.domain.CustomBuilderMenuItemBuilder) BiConsumer(java.util.function.BiConsumer) DefaultXmlPluginLoaderTest(com.thecoderscorner.menu.editorui.generator.plugin.DefaultXmlPluginLoaderTest) Test(org.junit.jupiter.api.Test)

Example 2 with ArduinoLibraryInstaller

use of com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller in project tcMenu by davetcc.

the class MenuEditorApp method start.

@Override
public void start(Stage primaryStage) throws Exception {
    startUpLogging();
    Platform.runLater(() -> {
        final String os = System.getProperty("os.name");
        if (os != null && os.startsWith("Mac")) {
            Desktop desktop = Desktop.getDesktop();
            desktop.setAboutHandler(e -> Platform.runLater(() -> controller.aboutMenuPressed(new ActionEvent())));
            desktop.setQuitStrategy(QuitStrategy.NORMAL_EXIT);
        }
    });
    ConfigurationStorage prefsStore = new PrefsConfigurationStorage();
    createOrUpdateDirectoriesAsNeeded(prefsStore);
    primaryStage.setTitle("Embedded Menu Designer");
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/ui/menuEditor.fxml"));
    Pane myPane = loader.load();
    controller = loader.getController();
    var stream = Preferences.userNodeForPackage(MenuEditorApp.class).get("ReleaseStream", ReleaseType.STABLE.toString());
    var httpClient = new SimpleHttpClient();
    var urlBase = "https://www.thecoderscorner.com";
    if (System.getProperty("localTccService") != null) {
        urlBase = System.getProperty("localTccService");
        System.getLogger("Main").log(WARNING, "Overriding the TCC service to " + urlBase);
    }
    LibraryVersionDetector libraryVersionDetector = new OnlineLibraryVersionDetector(urlBase, httpClient, ReleaseType.valueOf(stream));
    PluginEmbeddedPlatformsImpl platforms = new PluginEmbeddedPlatformsImpl();
    DefaultXmlPluginLoader manager = new DefaultXmlPluginLoader(platforms, prefsStore, true);
    ArduinoLibraryInstaller installer = new ArduinoLibraryInstaller(libraryVersionDetector, manager, prefsStore);
    platforms.setInstallerConfiguration(installer, prefsStore);
    manager.loadPlugins();
    var homeDirectory = System.getProperty("homeDirectoryOverride", System.getProperty("user.home"));
    var editorUI = new CurrentProjectEditorUIImpl(manager, primaryStage, platforms, installer, prefsStore, libraryVersionDetector, homeDirectory);
    FileBasedProjectPersistor persistor = new FileBasedProjectPersistor();
    CurrentEditorProject project = new CurrentEditorProject(editorUI, persistor, prefsStore);
    editorUI.setEditorProject(project);
    controller.initialise(project, installer, editorUI, manager, prefsStore, libraryVersionDetector);
    Scene myScene = new Scene(myPane);
    BaseDialogSupport.getJMetro().setScene(myScene);
    primaryStage.setScene(myScene);
    primaryStage.show();
    primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream("/img/menu-icon-sm.png"))));
    primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream("/img/menu-icon.png"))));
    primaryStage.setOnCloseRequest((evt) -> {
        try {
            var streamStr = libraryVersionDetector.getReleaseType().toString();
            Preferences.userNodeForPackage(MenuEditorApp.class).put("ReleaseStream", streamStr);
            controller.persistPreferences();
            if (project.isDirty()) {
                evt.consume();
                Alert alert = new Alert(AlertType.CONFIRMATION, "There are unsaved changes, save first?", ButtonType.YES, ButtonType.NO);
                BaseDialogSupport.getJMetro().setScene(alert.getDialogPane().getScene());
                alert.setTitle("Are you sure");
                alert.setHeaderText("");
                if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
                    project.saveProject(CurrentEditorProject.EditorSaveMode.SAVE);
                }
            }
        } catch (Exception ex) {
        // ignored, we are trying to shutdown so just proceeed anyway.
        }
        Platform.exit();
        System.exit(0);
    });
}
Also used : LibraryVersionDetector(com.thecoderscorner.menu.editorui.generator.LibraryVersionDetector) OnlineLibraryVersionDetector(com.thecoderscorner.menu.editorui.generator.OnlineLibraryVersionDetector) OnlineLibraryVersionDetector(com.thecoderscorner.menu.editorui.generator.OnlineLibraryVersionDetector) ActionEvent(javafx.event.ActionEvent) DefaultXmlPluginLoader(com.thecoderscorner.menu.editorui.generator.plugin.DefaultXmlPluginLoader) Scene(javafx.scene.Scene) Image(javafx.scene.image.Image) FXMLLoader(javafx.fxml.FXMLLoader) Pane(javafx.scene.layout.Pane) FileBasedProjectPersistor(com.thecoderscorner.menu.editorui.project.FileBasedProjectPersistor) IOException(java.io.IOException) PluginEmbeddedPlatformsImpl(com.thecoderscorner.menu.editorui.generator.plugin.PluginEmbeddedPlatformsImpl) ConfigurationStorage(com.thecoderscorner.menu.editorui.storage.ConfigurationStorage) PrefsConfigurationStorage(com.thecoderscorner.menu.editorui.storage.PrefsConfigurationStorage) CurrentProjectEditorUIImpl(com.thecoderscorner.menu.editorui.uimodel.CurrentProjectEditorUIImpl) ArduinoLibraryInstaller(com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller) Alert(javafx.scene.control.Alert) PrefsConfigurationStorage(com.thecoderscorner.menu.editorui.storage.PrefsConfigurationStorage) SimpleHttpClient(com.thecoderscorner.menu.editorui.util.SimpleHttpClient) CurrentEditorProject(com.thecoderscorner.menu.editorui.project.CurrentEditorProject)

Example 3 with ArduinoLibraryInstaller

use of com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller in project tcMenu by davetcc.

the class CodeGeneratorCommand method call.

@Override
public Integer call() {
    try {
        var project = projectFileOrNull(projectFile);
        System.out.format("Starting code generator for %s\n", project.getOptions().getApplicationName());
        var prefsStore = new PrefsConfigurationStorage();
        MenuEditorApp.createOrUpdateDirectoriesAsNeeded(prefsStore);
        prefsStore.setLastRunVersion(new VersionInfo(prefsStore.getVersion()));
        var platforms = new PluginEmbeddedPlatformsImpl();
        DefaultXmlPluginLoader loader = new DefaultXmlPluginLoader(platforms, prefsStore, true);
        loader.loadPlugins();
        platforms.setInstallerConfiguration(new ArduinoLibraryInstaller(new OfflineDetector(), loader, prefsStore), prefsStore);
        var embeddedPlatform = platforms.getEmbeddedPlatformFromId(project.getOptions().getEmbeddedPlatform());
        var codeGen = platforms.getCodeGeneratorFor(embeddedPlatform, project.getOptions());
        System.out.println("Preparing to execute generator");
        List<CodePluginItem> allPlugins = loader.getLoadedPlugins().stream().flatMap(pluginLib -> pluginLib.getPlugins().stream()).collect(Collectors.toList());
        List<CodePluginItem> plugins = new ArrayList<>();
        plugins.add(getPluginOrDefault(allPlugins, project.getOptions().getLastInputUuid(), DEFAULT_INPUT_PLUGIN));
        plugins.add(getPluginOrDefault(allPlugins, project.getOptions().getLastDisplayUuid(), DEFAULT_DISPLAY_PLUGIN));
        for (var plugin : project.getOptions().getLastRemoteCapabilitiesUuids()) {
            plugins.add(getPluginOrDefault(allPlugins, plugin, DEFAULT_REMOTE_PLUGIN));
        }
        if (project.getOptions().getLastThemeUuid() != null) {
            plugins.add(getPluginOrDefault(allPlugins, project.getOptions().getLastThemeUuid(), DEFAULT_THEME_PLUGIN));
        }
        System.out.format("Executing code generator");
        var location = Paths.get(loadedProjectFile.getParent());
        codeGen.setLoggerFunction((level, s) -> {
            if (verbose)
                System.out.format("Gen: %s: %s\n", level, s);
        });
        codeGen.startConversion(location, plugins, project.getMenuTree(), Collections.emptyList(), project.getOptions());
        return 0;
    } catch (Exception ex) {
        System.out.println("Error during code generation " + ex.getClass().getSimpleName() + " " + ex.getMessage());
        if (verbose) {
            ex.printStackTrace();
        }
        return -1;
    }
}
Also used : java.util(java.util) ProjectPersistor(com.thecoderscorner.menu.editorui.project.ProjectPersistor) Files(java.nio.file.Files) PluginEmbeddedPlatformsImpl(com.thecoderscorner.menu.editorui.generator.plugin.PluginEmbeddedPlatformsImpl) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) LibraryVersionDetector(com.thecoderscorner.menu.editorui.generator.LibraryVersionDetector) VersionInfo(com.thecoderscorner.menu.persist.VersionInfo) Collectors(java.util.stream.Collectors) File(java.io.File) MenuEditorApp(com.thecoderscorner.menu.editorui.MenuEditorApp) StandardCopyOption(java.nio.file.StandardCopyOption) ArduinoLibraryInstaller(com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller) CodePluginItem(com.thecoderscorner.menu.editorui.generator.plugin.CodePluginItem) Paths(java.nio.file.Paths) CodeGeneratorOptions(com.thecoderscorner.menu.editorui.generator.CodeGeneratorOptions) FileBasedProjectPersistor(com.thecoderscorner.menu.editorui.project.FileBasedProjectPersistor) PrefsConfigurationStorage(com.thecoderscorner.menu.editorui.storage.PrefsConfigurationStorage) Command(picocli.CommandLine.Command) DefaultXmlPluginLoader(com.thecoderscorner.menu.editorui.generator.plugin.DefaultXmlPluginLoader) MenuTree(com.thecoderscorner.menu.domain.state.MenuTree) OnlineLibraryVersionDetector(com.thecoderscorner.menu.editorui.generator.OnlineLibraryVersionDetector) MenuTreeWithCodeOptions(com.thecoderscorner.menu.editorui.project.MenuTreeWithCodeOptions) CommandLine(picocli.CommandLine) VersionInfo(com.thecoderscorner.menu.persist.VersionInfo) PluginEmbeddedPlatformsImpl(com.thecoderscorner.menu.editorui.generator.plugin.PluginEmbeddedPlatformsImpl) CodePluginItem(com.thecoderscorner.menu.editorui.generator.plugin.CodePluginItem) DefaultXmlPluginLoader(com.thecoderscorner.menu.editorui.generator.plugin.DefaultXmlPluginLoader) ArduinoLibraryInstaller(com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller) PrefsConfigurationStorage(com.thecoderscorner.menu.editorui.storage.PrefsConfigurationStorage) IOException(java.io.IOException)

Aggregations

ArduinoLibraryInstaller (com.thecoderscorner.menu.editorui.generator.arduino.ArduinoLibraryInstaller)3 MenuTree (com.thecoderscorner.menu.domain.state.MenuTree)2 LibraryVersionDetector (com.thecoderscorner.menu.editorui.generator.LibraryVersionDetector)2 OnlineLibraryVersionDetector (com.thecoderscorner.menu.editorui.generator.OnlineLibraryVersionDetector)2 DefaultXmlPluginLoader (com.thecoderscorner.menu.editorui.generator.plugin.DefaultXmlPluginLoader)2 PluginEmbeddedPlatformsImpl (com.thecoderscorner.menu.editorui.generator.plugin.PluginEmbeddedPlatformsImpl)2 FileBasedProjectPersistor (com.thecoderscorner.menu.editorui.project.FileBasedProjectPersistor)2 PrefsConfigurationStorage (com.thecoderscorner.menu.editorui.storage.PrefsConfigurationStorage)2 IOException (java.io.IOException)2 CustomBuilderMenuItemBuilder (com.thecoderscorner.menu.domain.CustomBuilderMenuItemBuilder)1 MenuEditorApp (com.thecoderscorner.menu.editorui.MenuEditorApp)1 CodeGeneratorOptions (com.thecoderscorner.menu.editorui.generator.CodeGeneratorOptions)1 CodeGeneratorOptionsBuilder (com.thecoderscorner.menu.editorui.generator.CodeGeneratorOptionsBuilder)1 ArduinoGenerator (com.thecoderscorner.menu.editorui.generator.arduino.ArduinoGenerator)1 ArduinoSketchFileAdjuster (com.thecoderscorner.menu.editorui.generator.arduino.ArduinoSketchFileAdjuster)1 IoExpanderDefinitionCollection (com.thecoderscorner.menu.editorui.generator.parameters.IoExpanderDefinitionCollection)1 ReadOnlyAuthenticatorDefinition (com.thecoderscorner.menu.editorui.generator.parameters.auth.ReadOnlyAuthenticatorDefinition)1 FlashRemoteId (com.thecoderscorner.menu.editorui.generator.parameters.auth.ReadOnlyAuthenticatorDefinition.FlashRemoteId)1 BspStm32EepromDefinition (com.thecoderscorner.menu.editorui.generator.parameters.eeprom.BspStm32EepromDefinition)1 CodePluginItem (com.thecoderscorner.menu.editorui.generator.plugin.CodePluginItem)1