Esempio n. 1
0
        /// <summary>
        /// Creates the Flutter module to be integrated into Xamarin (native) applications.
        /// </summary>
        private static DartProject CreateFlutterModule(NewProjectSettings settings, bool verbose = false)
        {
            try
            {
                DartProject project = FlutterTools.CreateModule(settings.FlutterSubfolderPath, settings.FlutterModuleName,
                                                                organization: settings.OrganizationId, verbose: verbose);

                FlutterTools.CreatePackage(settings.FlutterSubfolderPath, settings.FlutterPackageName,
                                           description: DartBridgeBuilder.DefaultPackageDescription, verbose: verbose);

                project.AddDevDependency(new DartProjectDependency(settings.FlutterPackageName, DartProjectDependencyType.Path, $"../{settings.FlutterPackageName}"));
                project.ApplyChanges();

                // Before building the module on a new machine, run "flutter pub get" first
                // to regenerate the .android/ and .ios/ directories.
                // (see: https://flutter.dev/docs/development/add-to-app/android/project-setup#create-a-flutter-module
                //  and: https://flutter.dev/docs/development/add-to-app/ios/project-setup#create-a-flutter-module)
                project.GetDependencies(verbose);

                return(project);
            }
            catch (Exception e)
            {
                throw new CommandLineException(CommandLineErrorCode.NewProject_CreateFlutterModuleFailed, e);
            }
        }
        private static void ConfigurePackageDependencies(DartProject prj, bool verbose = false, bool resetDefaultVersions = false)
        {
            if (verbose)
            {
                Console.WriteLine("Configuring package dependencies...");
            }
            else
            {
                Console.Write("Configuring package dependencies... ");
            }

            /*
             * dependencies:
             *  flutter:
             *    sdk: flutter
             # Your other regular dependencies here
             #  json_annotation: ^3.0.1
             #
             # Dependency for the project
             #  intl: ^0.15.7
             #  web_socket_channel: ^1.1.0
             #  synchronized: ^2.2.0+2
             #
             */

            List <DartProjectDependency> dependencies = new List <DartProjectDependency>();

            dependencies.Add(new DartProjectDependency("json_annotation", DartProjectDependencyType.Version, "^3.0.1"));
            dependencies.Add(new DartProjectDependency("intl", DartProjectDependencyType.Version, "^0.15.7"));
            dependencies.Add(new DartProjectDependency("web_socket_channel", DartProjectDependencyType.Version, "^1.1.0"));
            dependencies.Add(new DartProjectDependency("synchronized", DartProjectDependencyType.Version, "^2.2.0+2"));

            // Add the dependencies to the project
            dependencies.ForEach(d =>
            {
                if (resetDefaultVersions)
                {
                    prj.AddDependency(d, true);
                }
                else
                {
                    // Override the dependency version only if it's greater than the current one defined inside pubspec
                    bool overrideVersion = true;
                    try
                    {
                        DartProjectDependency currentDependency = prj.GetCurrentDependency(d);
                        if (currentDependency != null && currentDependency.Value.ToNetVersion().CompareTo(d.Value.ToNetVersion()) > 0)
                        {
                            overrideVersion = false;
                        }
                    }
                    finally
                    {
                        prj.AddDependency(d, overrideVersion);
                    }
                }
            });

            /*
             * dev_dependencies:
             *  build_runner: ^1.10.0
             *  json_serializable: ^3.3.0
             */

            List <DartProjectDependency> devDependencies = new List <DartProjectDependency>();

            devDependencies.Add(new DartProjectDependency("build_runner", DartProjectDependencyType.Version, "^1.10.0"));
            devDependencies.Add(new DartProjectDependency("json_serializable", DartProjectDependencyType.Version, "^3.3.0"));

            // Add the DEV dependencies to the project
            devDependencies.ForEach(d =>
            {
                if (resetDefaultVersions)
                {
                    prj.AddDevDependency(d, true);
                }
                else
                {
                    // Override the DEV dependency version only if it's greater than the current one defined inside pubspec
                    bool overrideVersion = true;
                    try
                    {
                        DartProjectDependency currentDependency = prj.GetCurrentDevDependency(d);
                        if (currentDependency != null && currentDependency.Value.ToNetVersion().CompareTo(d.Value.ToNetVersion()) > 0)
                        {
                            overrideVersion = false;
                        }
                    }
                    finally
                    {
                        prj.AddDevDependency(d, overrideVersion);
                    }
                }
            });

            // Set Dart SDK version
            if (verbose)
            {
                Console.WriteLine("Configuring Dart SDK version...");
            }
            prj.SetEnvironmentSdk(">=2.6.0 <3.0.0");

            // Save changes to file 'pubspec.yaml' and run 'flutter pub get' if required
            bool changed = prj.ApplyChanges();

            if (changed)
            {
                if (verbose)
                {
                    Console.WriteLine("Resolving all dependencies...");
                }
                FlutterTools.GetDependencies(prj.WorkingDir.FullName, verbose);
            }

            if (verbose)
            {
                Console.WriteLine("Done.");
            }
            else
            {
                Console.Write("Done.");
            }
            Console.WriteLine();
        }