Class: Makit::DotNet::SolutionMaui

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/dotnet/solution_maui.rb

Class Method Summary collapse

Class Method Details

.generate_gitlab_ci(project_name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/makit/dotnet/solution_maui.rb', line 167

def self.generate_gitlab_ci(project_name)
  gitlab_ci_content = <<~YAML
    # #{project_name} MAUI Project GitLab CI/CD Pipeline
    # frozen_string_literal: true

    stages:
      - build
      - test
      - publish

    variables:
      DOTNET_VERSION: "8.0.x"
      PROJECT_PATH: "source/#{project_name}/#{project_name}.csproj"
      BUILD_CONFIGURATION: "Release"
      TEST_RESULTS_DIR: "test-results"
      COVERAGE_DIR: "coverage"
      PUBLISH_DIR: "artifacts/publish"

    # Cache NuGet packages between jobs
    cache:
      paths:
        - "**/bin/"
        - "**/obj/"
        - "**/packages/"
        - "~/.nuget/packages/"

    # Build job
    build:
      stage: build
      image: mcr.microsoft.com/dotnet/sdk:8.0
      script:
        - echo "Building #{project_name} MAUI project..."
        - dotnet --version
        - dotnet restore $PROJECT_PATH
        - dotnet build $PROJECT_PATH --configuration $BUILD_CONFIGURATION --no-restore
        - echo "MAUI project build completed successfully!"
      artifacts:
        paths:
          - "**/bin/"
          - "**/obj/"
        expire_in: 1 week
        when: on_success
      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        - if: $CI_COMMIT_TAG

    # Test job
    test:
      stage: test
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - build
      script:
        - echo "Testing #{project_name} MAUI project..."
        - dotnet test $PROJECT_PATH --configuration $BUILD_CONFIGURATION --no-build --verbosity normal
        - echo "MAUI project tests completed successfully!"
      artifacts:
        paths:
          - $TEST_RESULTS_DIR/
          - $COVERAGE_DIR/
        reports:
          junit: $TEST_RESULTS_DIR/*.xml
        expire_in: 1 week
        when: always
      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        - if: $CI_COMMIT_TAG

    # Publish Android job
    publish_android:
      stage: publish
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - test
      script:
        - echo "Publishing #{project_name} MAUI project for Android..."
        - mkdir -p $PUBLISH_DIR
        - dotnet publish $PROJECT_PATH --configuration $BUILD_CONFIGURATION --framework net8.0-android --output $PUBLISH_DIR/#{project_name}-android
        - echo "Android publish completed successfully!"
      artifacts:
        paths:
          - $PUBLISH_DIR/#{project_name}-android/
        expire_in: 1 month
        when: on_success
      rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        - if: $CI_COMMIT_TAG

    # Publish iOS job
    publish_ios:
      stage: publish
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - test
      script:
        - echo "Publishing #{project_name} MAUI project for iOS..."
        - mkdir -p $PUBLISH_DIR
        - dotnet publish $PROJECT_PATH --configuration $BUILD_CONFIGURATION --framework net8.0-ios --output $PUBLISH_DIR/#{project_name}-ios
        - echo "iOS publish completed successfully!"
      artifacts:
        paths:
          - $PUBLISH_DIR/#{project_name}-ios/
        expire_in: 1 month
        when: on_success
      rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        - if: $CI_COMMIT_TAG

    # Publish Windows job
    publish_windows:
      stage: publish
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - test
      script:
        - echo "Publishing #{project_name} MAUI project for Windows..."
        - mkdir -p $PUBLISH_DIR
        - dotnet publish $PROJECT_PATH --configuration $BUILD_CONFIGURATION --framework net8.0-windows10.0.19041.0 --output $PUBLISH_DIR/#{project_name}-windows
        - echo "Windows publish completed successfully!"
      artifacts:
        paths:
          - $PUBLISH_DIR/#{project_name}-windows/
        expire_in: 1 month
        when: on_success
      rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        - if: $CI_COMMIT_TAG

    # Publish macOS job
    publish_macos:
      stage: publish
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - test
      script:
        - echo "Publishing #{project_name} MAUI project for macOS..."
        - mkdir -p $PUBLISH_DIR
        - dotnet publish $PROJECT_PATH --configuration $BUILD_CONFIGURATION --framework net8.0-maccatalyst --output $PUBLISH_DIR/#{project_name}-macos
        - echo "macOS publish completed successfully!"
      artifacts:
        paths:
          - $PUBLISH_DIR/#{project_name}-macos/
        expire_in: 1 month
        when: on_success
      rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        - if: $CI_COMMIT_TAG

    # Code quality job (optional)
    code_quality:
      stage: test
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - build
      script:
        - echo "Running code quality checks..."
        - dotnet tool install --global dotnet-format
        - dotnet format --verify-no-changes $PROJECT_PATH
        - echo "Code quality checks completed!"
      allow_failure: true
      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

    # Security scan job (optional)
    security_scan:
      stage: test
      image: mcr.microsoft.com/dotnet/sdk:8.0
      dependencies:
        - build
      script:
        - echo "Running security scan..."
        - dotnet tool install --global dotnet-outdated-tool
        - dotnet outdated $PROJECT_PATH
        - echo "Security scan completed!"
      allow_failure: true
      rules:
        - if: $CI_PIPELINE_SOURCE == "merge_request_event"
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  YAML

  File.write(".gitlab-ci.yml", gitlab_ci_content)
end

.generate_rakefile(project_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/makit/dotnet/solution_maui.rb', line 16

def self.generate_rakefile(project_name)
  rakefile_content = <<~RUBY
    # frozen_string_literal: true

    require "makit"

    #
    # #{project_name} MAUI Project Rakefile
    #
    desc "default task - builds and tests the MAUI project"
    task :default => [:build, :test]

    desc "build the #{project_name} MAUI project"
    task :build do
      puts "Building #{project_name} MAUI project..."
      project_path = "source/#{project_name}/#{project_name}.csproj"
    #{"  "}
      if File.exist?(project_path)
        sh "dotnet build \#{project_path} --configuration Release"
        puts "MAUI project build completed successfully!"
      else
        puts "Error: Project file not found at \#{project_path}"
        exit 1
      end
    end

    desc "test the #{project_name} MAUI project"
    task :test do
      puts "Testing #{project_name} MAUI project..."
      project_path = "source/#{project_name}/#{project_name}.csproj"
    #{"  "}
      if File.exist?(project_path)
        sh "dotnet test \#{project_path} --configuration Release --verbosity normal"
        puts "MAUI project tests completed successfully!"
      else
        puts "Error: Project file not found at \#{project_path}"
        exit 1
      end
    end

    desc "run the #{project_name} MAUI project on Android"
    task :run_android => [:build] do
      puts "Running #{project_name} MAUI project on Android..."
      project_path = "source/#{project_name}/#{project_name}.csproj"
    #{"  "}
      if File.exist?(project_path)
        sh "dotnet build \#{project_path} -t:Run -f net8.0-android"
      else
        puts "Error: Project file not found at \#{project_path}"
        exit 1
      end
    end

    desc "run the #{project_name} MAUI project on iOS"
    task :run_ios => [:build] do
      puts "Running #{project_name} MAUI project on iOS..."
      project_path = "source/#{project_name}/#{project_name}.csproj"
    #{"  "}
      if File.exist?(project_path)
        sh "dotnet build \#{project_path} -t:Run -f net8.0-ios"
      else
        puts "Error: Project file not found at \#{project_path}"
        exit 1
      end
    end

    desc "run the #{project_name} MAUI project on Windows"
    task :run_windows => [:build] do
      puts "Running #{project_name} MAUI project on Windows..."
      project_path = "source/#{project_name}/#{project_name}.csproj"
    #{"  "}
      if File.exist?(project_path)
        sh "dotnet build \#{project_path} -t:Run -f net8.0-windows10.0.19041.0"
      else
        puts "Error: Project file not found at \#{project_path}"
        exit 1
      end
    end

    desc "run the #{project_name} MAUI project on macOS"
    task :run_macos => [:build] do
      puts "Running #{project_name} MAUI project on macOS..."
      project_path = "source/#{project_name}/#{project_name}.csproj"
    #{"  "}
      if File.exist?(project_path)
        sh "dotnet build \#{project_path} -t:Run -f net8.0-maccatalyst"
      else
        puts "Error: Project file not found at \#{project_path}"
        exit 1
      end
    end

    desc "clean build artifacts"
    task :clean do
      puts "Cleaning MAUI project build artifacts..."
      sh "dotnet clean source/#{project_name}/#{project_name}.csproj"
      puts "Clean completed successfully!"
    end

    desc "restore packages"
    task :restore do
      puts "Restoring MAUI project packages..."
      sh "dotnet restore source/#{project_name}/#{project_name}.csproj"
      puts "Restore completed successfully!"
    end

    desc "publish the MAUI project for Android"
    task :publish_android do
      puts "Publishing #{project_name} MAUI project for Android..."
      output_dir = "artifacts/publish/#{project_name}-android"
      FileUtils.mkdir_p(output_dir)
      sh "dotnet publish source/#{project_name}/#{project_name}.csproj --configuration Release --framework net8.0-android --output \#{output_dir}"
      puts "Android publish completed successfully! Output: \#{output_dir}"
    end

    desc "publish the MAUI project for iOS"
    task :publish_ios do
      puts "Publishing #{project_name} MAUI project for iOS..."
      output_dir = "artifacts/publish/#{project_name}-ios"
      FileUtils.mkdir_p(output_dir)
      sh "dotnet publish source/#{project_name}/#{project_name}.csproj --configuration Release --framework net8.0-ios --output \#{output_dir}"
      puts "iOS publish completed successfully! Output: \#{output_dir}"
    end

    desc "publish the MAUI project for Windows"
    task :publish_windows do
      puts "Publishing #{project_name} MAUI project for Windows..."
      output_dir = "artifacts/publish/#{project_name}-windows"
      FileUtils.mkdir_p(output_dir)
      sh "dotnet publish source/#{project_name}/#{project_name}.csproj --configuration Release --framework net8.0-windows10.0.19041.0 --output \#{output_dir}"
      puts "Windows publish completed successfully! Output: \#{output_dir}"
    end

    desc "publish the MAUI project for macOS"
    task :publish_macos do
      puts "Publishing #{project_name} MAUI project for macOS..."
      output_dir = "artifacts/publish/#{project_name}-macos"
      FileUtils.mkdir_p(output_dir)
      sh "dotnet publish source/#{project_name}/#{project_name}.csproj --configuration Release --framework net8.0-maccatalyst --output \#{output_dir}"
      puts "macOS publish completed successfully! Output: \#{output_dir}"
    end

    desc "publish for all supported platforms"
    task :publish_all => [:publish_android, :publish_ios, :publish_windows, :publish_macos] do
      puts "All platform publishing completed successfully!"
    end
  RUBY

  File.write("Rakefile", rakefile_content)
end

.setup(name) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/makit/dotnet/solution_maui.rb', line 6

def self.setup(name)
  Makit::DotNet::Project.new_project("maui", name, "source/#{name}", "--framework net8.0")
  Makit::DotNet::Project.new_project("TUnit", "#{name}.Tests", "tests/#{name}")
  # Makit::DotNet::Project.add_reference("tests/#{name}/#{name}.Tests.csproj", "source/#{name}/#{name}.csproj")

  # Generate additional project files
  generate_rakefile(name)
  generate_gitlab_ci(name)
end