Class: WayOfWorking::CodeLinting::Hdi::Generators::InitLinters

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions, GithubMetadata
Defined in:
lib/way_of_working/code_linting/hdi/generators/init_linters.rb

Overview

This generator initialises the linter

Constant Summary collapse

LINTING_BUILD_PHASE =
"				2F0882F42AAB152D00DB0B2B /* ShellScript */,\n"
LINTING_BUILD_PHASE_DETAILS =
<<~CONFIG
  /* Begin PBXShellScriptBuildPhase section */
  \t\t2F0882F42AAB152D00DB0B2B /* ShellScript */ = {
  \t\t\tisa = PBXShellScriptBuildPhase;
  \t\t\tbuildActionMask = 2147483647;
  \t\t\tfiles = (
  \t\t\t);
  \t\t\tinputFileListPaths = (
  \t\t\t);
  \t\t\tinputPaths = (
  \t\t\t);
  \t\t\toutputFileListPaths = (
  \t\t\t);
  \t\t\toutputPaths = (
  \t\t\t);
  \t\t\trunOnlyForDeploymentPostprocessing = 0;
  \t\t\tshellPath = /bin/sh;
  \t\t\tshellScript = "if [[ \\"$(uname -m)\\" == arm64 ]]; then\\n    export PATH=\\"/opt/homebrew/bin:$PATH\\"\\nfi\\n\\nif which swiftlint > /dev/null; then\\n  swiftlint\\nelse\\n  echo \\"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\\"\\nfi\\n";
  \t\t};
  /* End PBXShellScriptBuildPhase section */

CONFIG

Instance Method Summary collapse

Instance Method Details

#configure_eslintObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 81

def configure_eslint
  if javascript_files_present?
    protect_and_copy_file '.eslintrc.js'

    # We don't have an eslintignore file in the repo, but we want to protect it in CODEOWNERS
    append_foldername_to_file_if_folder_exists '.eslintignore', 'coverage'
    append_to_file '.eslintignore', "megalinter-reports/\n"
    append_foldername_to_file_if_folder_exists '.eslintignore', 'node_modules'
    append_foldername_to_file_if_folder_exists '.eslintignore', 'public'
    append_foldername_to_file_if_folder_exists '.eslintignore', 'vendor'
    protect_files_in_codeowners '.eslintignore'

    run 'npm install --save-dev ' \
        'eslint-config-standard@^17.1.0 ' \
        'eslint-plugin-cypress@^3.6.0 ' \
        'eslint-plugin-jasmine@^4.2.2'
  else
    say 'No JavaScript files found, skipping ESLint configuration'
  end
end

#configure_markdownlintObject



74
75
76
77
78
79
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 74

def configure_markdownlint
  # We don't have these files in the repo, but we want to protect them in CODEOWNERS
  protect_files_in_codeowners '.markdownlintignore', '.markdownlint.*'

  prepend_to_file_if_exists 'CHANGELOG.md', "<!-- markdownlint-disable-file MD024 -->\n"
end


70
71
72
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 70

def copy_github_linters_markdown_link_check_config_file
  copy_file '.github/linters/.markdown-link-check.json'
end

#copy_github_linters_rubocop_config_fileObject



66
67
68
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 66

def copy_github_linters_rubocop_config_file
  copy_file '.github/linters/rubocop_defaults.yml'
end

#copy_megalinter_dot_fileObject



106
107
108
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 106

def copy_megalinter_dot_file
  protect_and_copy_file '.mega-linter.yml'
end

#copy_megalinter_github_workflow_actionObject



102
103
104
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 102

def copy_megalinter_github_workflow_action
  protect_and_copy_file '.github/workflows/mega-linter.yml'
end

#copy_rubocop_options_fileObject



122
123
124
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 122

def copy_rubocop_options_file
  protect_and_copy_file '.rubocop'
end

#create_gitignore_if_missingObject



110
111
112
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 110

def create_gitignore_if_missing
  create_file_if_missing '.gitignore'
end

#gitignore_reports_folderObject



114
115
116
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 114

def gitignore_reports_folder
  append_to_file '.gitignore', "megalinter-reports/\n"
end

#gitignore_rubocop_cached_fileObject



118
119
120
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 118

def gitignore_rubocop_cached_file
  append_to_file '.gitignore', ".rubocop-https---*\n"
end

#give_code_standards_team_rw_access_to_repoObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 43

def give_code_standards_team_rw_access_to_repo
  return unless github_organisation && github_full_repo_name

  client = octokit_client

  say "Adding #{code_standards_team} team with write access to #{github_full_repo_name}..."

  client.add_team_repository(
    "#{github_organisation}/#{code_standards_team}",
    github_full_repo_name,
    permission: 'push'
  )

  say "Successfully granted write access to #{code_standards_team} team", :green
rescue Octokit::Error => e
  say "Warning: Could not add team to repository: #{e.message}", :yellow
  say 'You may need to configure this manually or ensure you have proper GitHub permissions', :yellow
end

#inject_swiftlint_into_xcode_project_build_processObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 126

def inject_swiftlint_into_xcode_project_build_process
  return unless xcode_project_file && File.exist?(xcode_project_file)

  inject_into_file xcode_project_file,
                   LINTING_BUILD_PHASE,
                   after: "buildPhases = (\n"

  inject_into_file xcode_project_file,
                   LINTING_BUILD_PHASE_DETAILS,
                   after: "/* End PBXResourcesBuildPhase section */\n\n"
end

#protect_github_linters_directoryObject



62
63
64
# File 'lib/way_of_working/code_linting/hdi/generators/init_linters.rb', line 62

def protect_github_linters_directory
  protect_files_in_codeowners '/.github/linters/'
end