// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_ARCH = "avr";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_VENDOR = "arduino";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/pic32/PIC32Platform.java
// public class PIC32Platform extends Platform {
//     
//     public PIC32Platform(Platform parent, String vendor, Path rootPath) throws IOException {
//         super(parent, vendor, "pic32", rootPath );
//         putValue("compiler.c.cmd", "xc32-gcc");
//         putValue("compiler.c.elf.cmd", "xc32-g++");
//         putValue("compiler.cpp.cmd", "xc32-g++");
//         putValue("compiler.ar.cmd", "xc32-ar");
//         putValue("compiler.objcopy.cmd", "xc32-objcopy");
//         putValue("compiler.elf2hex.cmd", "xc32-bin2hex");
//         putValue("compiler.size.cmd", "xc32-size");
//         data.entrySet().forEach( e -> e.setValue( e.getValue().replaceAll(" -O2 ", " -O1 ") ) );
//     }
//     
// }


import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_ARCH;
import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_VENDOR;
import com.microchip.mplab.nbide.embedded.arduino.importer.pic32.PIC32Platform;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
package com.microchip.mplab.nbide.embedded.arduino.importer;



public final class PlatformFactory {

    public static final String BOARDS_FILENAME = "boards.txt";
    public static final String PLATFORM_FILENAME = "platform.txt";

    private static final Logger LOGGER = Logger.getLogger(PlatformFactory.class.getName());

    private final List<Platform> allPlatforms = new ArrayList<>();

    public List<Platform> getAllPlatforms(Path arduinoSettingsPath) throws IOException {
        if (allPlatforms.isEmpty()) {

            Path settingsPath = validateArduinoSettingsPath(arduinoSettingsPath);

            // Find all paths containing a "platform.txt" file
            LOGGER.log(Level.INFO, "Searching for platform files in {0}", settingsPath);
            FileFinder finder = new FileFinder(PLATFORM_FILENAME);
            Files.walkFileTree(settingsPath, finder);
            List<Path> platformPaths = finder.getMatchingPaths();

            Platform rootPlatform = createRootPlatform();
            if ( rootPlatform == null ) {
                throw new RuntimeException("Failed to load the root platform!");
            }

            platformPaths.stream().map(path -> createPlatformFromFile(rootPlatform, path)).forEach(allPlatforms::add);
            
            // Add the root platform but only if there is no platform in the user directory with the same vendor/arch:
            if ( !allPlatforms.stream().anyMatch( 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_ARCH = "avr";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/ArduinoConfig.java
// public static final String ROOT_PLATFORM_VENDOR = "arduino";
// 
// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/pic32/PIC32Platform.java
// public class PIC32Platform extends Platform {
//     
//     public PIC32Platform(Platform parent, String vendor, Path rootPath) throws IOException {
//         super(parent, vendor, "pic32", rootPath );
//         putValue("compiler.c.cmd", "xc32-gcc");
//         putValue("compiler.c.elf.cmd", "xc32-g++");
//         putValue("compiler.cpp.cmd", "xc32-g++");
//         putValue("compiler.ar.cmd", "xc32-ar");
//         putValue("compiler.objcopy.cmd", "xc32-objcopy");
//         putValue("compiler.elf2hex.cmd", "xc32-bin2hex");
//         putValue("compiler.size.cmd", "xc32-size");
//         data.entrySet().forEach( e -> e.setValue( e.getValue().replaceAll(" -O2 ", " -O1 ") ) );
//     }
//     
// }


// Path: src/com/microchip/mplab/nbide/embedded/arduino/importer/PlatformFactory.java
import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_ARCH;
import static com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoConfig.ROOT_PLATFORM_VENDOR;
import com.microchip.mplab.nbide.embedded.arduino.importer.pic32.PIC32Platform;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

package com.microchip.mplab.nbide.embedded.arduino.importer;



public final class PlatformFactory {

    public static final String BOARDS_FILENAME = "boards.txt";
    public static final String PLATFORM_FILENAME = "platform.txt";

    private static final Logger LOGGER = Logger.getLogger(PlatformFactory.class.getName());

    private final List<Platform> allPlatforms = new ArrayList<>();

    public List<Platform> getAllPlatforms(Path arduinoSettingsPath) throws IOException {
        if (allPlatforms.isEmpty()) {

            Path settingsPath = validateArduinoSettingsPath(arduinoSettingsPath);

            // Find all paths containing a "platform.txt" file
            LOGGER.log(Level.INFO, "Searching for platform files in {0}", settingsPath);
            FileFinder finder = new FileFinder(PLATFORM_FILENAME);
            Files.walkFileTree(settingsPath, finder);
            List<Path> platformPaths = finder.getMatchingPaths();

            Platform rootPlatform = createRootPlatform();
            if ( rootPlatform == null ) {
                throw new RuntimeException("Failed to load the root platform!");
            }

            platformPaths.stream().map(path -> createPlatformFromFile(rootPlatform, path)).forEach(allPlatforms::add);
            
            // Add the root platform but only if there is no platform in the user directory with the same vendor/arch:
            if ( !allPlatforms.stream().anyMatch( 
                platform -> ROOT_PLATFORM_VENDOR.equalsIgnoreCase(platform.getVendor()) && ROOT_PLATFORM_ARCH.equalsIgnoreCase(platform.getArchitecture())