import groovy.text.SimpleTemplateEngine import org.sonar.build.BlackBoxTest import static org.gradle.api.JavaVersion.VERSION_17 plugins { // Ordered alphabetically id 'com.github.hierynomus.license' version '0.16.1' id "com.github.hierynomus.license-report" version "0.16.1" apply false id 'com.gradleup.shadow' version '8.3.3' apply false id 'com.google.protobuf' version '0.8.19' apply false // dont update com.jfrog.artifactory, new version contains a bug id 'com.jfrog.artifactory' version '5.2.5' id "de.undercouch.download" version "5.6.0" apply false id 'io.spring.dependency-management' version '1.1.6' id "org.cyclonedx.bom" version "1.10.0" apply false id 'org.sonarqube' version '5.1.0.4882' } if (!JavaVersion.current().isCompatibleWith(VERSION_17)) { throw new GradleException("JDK 17+ is required to perform this build. It's currently " + System.getProperty("java.home") + ".") } /** * The BOM related tasks are disabled by default, activated by: * - running in the CI and being on a main branch or a nightly build, * - or using '-Dbom' project property * - or by explicit call to 'cyclonedxBom' Gradle task */ def bomTasks = "cyclonedxBom" def ghBranch = System.getenv()["GITHUB_BRANCH"] def isMainBranch = ghBranch in ['master'] || ghBranch ==~ 'branch-[\\d.]+' boolean enableBom = System.getenv('CI') == "true" && (isMainBranch) || System.getProperty("bom") != null || gradle.startParameter.taskNames.findAll({ it.matches(".*:($bomTasks)") }) allprojects { apply plugin: 'com.jfrog.artifactory' apply plugin: 'maven-publish' ext.versionInSources = version ext.buildNumber = System.getProperty("buildNumber") // when no buildNumber is provided, then project version must end with '-SNAPSHOT' if (ext.buildNumber == null) { version = "${version}-SNAPSHOT".toString() ext.versionWithoutBuildNumber = version } else { ext.versionWithoutBuildNumber = version version = (version.toString().count('.') == 1 ? "${version}.0.${ext.buildNumber}" : "${version}.${ext.buildNumber}").toString() } task cacheDependencies { doLast { configurations.each { conf -> if (conf.isCanBeResolved()) { if (conf.getName() != 'appZip') conf.resolve() } } } } ext { release = project.hasProperty('release') && project.getProperty('release') deployCommunity = project.hasProperty('deployCommunity') && (project.getProperty('deployCommunity') == 'true') official = project.hasProperty('official') && project.getProperty('official') } ext.enableBom = enableBom if (!enableBom) { tasks.matching { it.name.matches(bomTasks) }.all({ logger.info("{} disabled", it.name); it.enabled = false }) } repositories { def repository = project.hasProperty('qa') ? 'sonarsource-qa' : 'sonarsource' // The environment variables ARTIFACTORY_PRIVATE_USERNAME and ARTIFACTORY_PRIVATE_PASSWORD are used on QA env (Jenkins) // On local box, please add artifactoryUsername and artifactoryPassword to ~/.gradle/gradle.properties def artifactoryUsername = System.env.'ARTIFACTORY_PRIVATE_USERNAME' ?: (project.hasProperty('artifactoryUsername') ? project.getProperty('artifactoryUsername') : '') def artifactoryPassword = System.env.'ARTIFACTORY_PRIVATE_PASSWORD' ?: (project.hasProperty('artifactoryPassword') ? project.getProperty('artifactoryPassword') : '') def artifactoryUrl = System.getenv('ARTIFACTORY_URL') ?: (project.hasProperty('artifactoryUrl') ? project.getProperty('artifactoryUrl') : '') if (artifactoryPassword) { if (artifactoryUrl == '') { throw new GradleException('Invalid artifactoryUrl') } maven { authentication { header(HttpHeaderAuthentication) } credentials(HttpHeaderCredentials) { name = "Authorization" value = "Bearer $artifactoryPassword" } url "${artifactoryUrl}/${repository}" } } else { mavenCentral() maven { url 'https://jitpack.io' } maven { url 'https://maven.codelibs.org/' } } ivy { if (artifactoryUsername && artifactoryPassword) { url "${artifactoryUrl}/sonarsource-bucket" authentication { header(HttpHeaderAuthentication) } credentials(HttpHeaderCredentials) { name = "Authorization" value = "Bearer $artifactoryPassword" } patternLayout { artifact '/[organisation]/[module]/[module]-[revision].[ext]' } } else { // For public build url "https://artifacts.elastic.co/downloads/" patternLayout { artifact '/[organisation]/[module]-[revision].[ext]' } } metadataSources { artifact() } } } task allDependencies { dependsOn 'dependencies' } artifactory { clientConfig.setIncludeEnvVars(true) clientConfig.setEnvVarsExcludePatterns('*pass*,*psw*,*secret*,*MAVEN_CMD_LINE_ARGS*,sun.java.command,*token*,*login*,*key*,*signing*,*auth*,*pwd*') clientConfig.publisher.setFilterExcludedArtifactsFromBuild(true) contextUrl = System.getenv('ARTIFACTORY_URL') //if property deployCommunity is true set the value to sonar-enterprise-sqcb //otherwise set it to sqs if (deployCommunity) { clientConfig.info.setBuildName('sonar-enterprise-sqcb') clientConfig.info.addEnvironmentProperty('ARTIFACTS_TO_PUBLISH', "${project.group}:sonar-application:zip,") } else { clientConfig.info.setBuildName('sonar-enterprise-sqs') clientConfig.info.addEnvironmentProperty('ARTIFACTS_TO_PUBLISH', "com.sonarsource.sonarqube:sonarqube-developer:zip," + "com.sonarsource.sonarqube:sonarqube-datacenter:zip," + "com.sonarsource.sonarqube:sonarqube-enterprise:zip") } publish { repository { repoKey = System.getenv('ARTIFACTORY_DEPLOY_REPO') username = System.getenv('ARTIFACTORY_DEPLOY_USERNAME') ?: project.properties.artifactoryUsername password = System.getenv('ARTIFACTORY_DEPLOY_PASSWORD') ?: project.properties.artifactoryPaswword } defaults { properties = [ 'build.name' : 'sonar-enterprise-sqcb', 'build.number' : System.getenv('BUILD_NUMBER'), 'pr.branch.target': System.getenv('GITHUB_BASE_BRANCH'), 'pr.number' : System.getenv('PULL_REQUEST'), 'vcs.branch' : ghBranch, 'vcs.revision' : System.getenv('GIT_SHA1'), 'version' : version ] publications('mavenJava') publishPom = true publishIvy = false } } clientConfig.info.setBuildNumber(System.getenv('BUILD_NUMBER')) // The name of this variable is important because it's used by the delivery process when extracting version from Artifactory build info. clientConfig.info.addEnvironmentProperty('PROJECT_VERSION', "${version}") } } apply plugin: 'org.sonarqube' sonar { properties { property 'sonar.projectName', projectTitle property 'sonar.projectVersion', "${versionInSources}-SNAPSHOT" property 'sonar.buildString', version } } tasks.named('wrapper') { distributionType = Wrapper.DistributionType.ALL } subprojects { apply plugin: 'com.github.hierynomus.license' apply plugin: 'io.spring.dependency-management' apply plugin: 'jacoco' apply plugin: 'java-library' apply plugin: 'idea' apply plugin: 'signing' // do not deploy to Artifactory by default artifactoryPublish.skip = true compileJava.options.encoding = "UTF-8" compileTestJava.options.encoding = "UTF-8" def testFixtureSrc = 'src/testFixtures' if (file(testFixtureSrc).exists()) { apply plugin: 'java-test-fixtures' } ext { protobufVersion = '4.28.2' springVersion
import {basename, extname, isObject, isDarkTheme} from '../utils.js';

const languagesByFilename = {};
const languagesByExt = {};

function getEditorconfig(input) {
  try {
    return JSON.parse(input.dataset.editorconfig);
  } catch {
    return null;
  }
}

function initLanguages(monaco) {
  for (const {filenames, extensions, id} of monaco.languages.getLanguages()) {
    for (const filename of filenames || []) {
      languagesByFilename[filename] = id;
    }
    for (const extension of extensions || []) {
      languagesByExt[extension] = id;
    }
  }
}

function getLanguage(filename) {
  return languagesByFilename[filename] || languagesByExt[extname(filename)] || 'plaintext';
}

function updateEditor(monaco, editor, filenameInput) {
  const newFilename = filenameInput.value;
  editor.updateOptions(getOptions(filenameInput));
  const model = editor.getModel();
  const language = model.getModeId();
  const newLanguage = getLanguage(newFilename);
  if (language !== newLanguage) monaco.editor.setModelLanguage(model, newLanguage);
}

// export editor for customization - https://github.com/go-gitea/gitea/issues/10409
function exportEditor(editor) {
  if (!window.codeEditors) window.codeEditors = [];
  if (!window.codeEditors.includes(editor)) window.codeEditors.push(editor);
}

export async function createCodeEditor(textarea, filenameInput, previewFileModes) {
  const filename = basename(filenameInput.value);
  const previewLink = document.querySelector('a[data-tab=preview]');
  const markdownExts = (textarea.dataset.markdownFileExts || '').split(',');
  const lineWrapExts = (textarea.dataset.lineWrapExtensions || '').split(',');
  const isMarkdown = markdownExts.includes(extname(filename));

  if (previewLink) {
    if (isMarkdown && (previewFileModes || []).includes('markdown')) {
      previewLink.dataset.url = previewLink.dataset.url.replace(/(.*)\/.*/i, `$1/markdown`);
      previewLink.style.display = '';
    } else {
      previewLink.style.display = 'none';
    }
  }

  const monaco = await import(/* webpackChunkName: "monaco" */'monaco-editor');
  initLanguages(monaco);

  const container = document.createElement('div');
  container.className = 'monaco-editor-container';
  textarea.parentNode.appendChild(container);

  const editor = monaco.editor.create(container, {
    value: textarea.value,
    language: getLanguage(filename),
    ...getOptions(filenameInput, lineWrapExts),
  });

  const model = editor.getModel();
  model.onDidChangeContent(() => {
    textarea.value = editor.getValue();
    textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery-are-you-sure
  });

  window.addEventListener('resize', () => {
    editor.layout();
  });

  filenameInput.addEventListener('keyup', () => {
    updateEditor(monaco, editor, filenameInput);
  });

  const loading = document.querySelector('.editor-loading');
  if (loading) loading.remove();

  exportEditor(editor);

  return editor;
}

function getOptions(filenameInput, lineWrapExts) {
  const ec = getEditorconfig(filenameInput);
  const theme = isDarkTheme() ? 'vs-dark' : 'vs';
  const wordWrap = (lineWrapExts || []).includes(extname(filenameInput.value)) ? 'on' : 'off';

  const opts = {theme, wordWrap};
  if (isObject(ec)) {
    opts.detectIndentation = !('indent_style' in ec) || !('indent_size' in ec);
    if ('indent_size' in ec) opts.indentSize = Number(ec.indent_size);
    if ('tab_width' in ec) opts.tabSize = Number(ec.tab_width) || opts.indentSize;
    if ('max_line_length' in ec) opts.rulers = [Number(ec.max_line_length)];
    opts.trimAutoWhitespace = ec.trim_trailing_whitespace === true;
    opts.insertSpaces = ec.indent_style === 'space';
    opts.useTabStops = ec.indent_style === 'tab';
  }

  return opts;
}