question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The coalesce macro should work with built-in dotnet new parameters

See original GitHub issue

I’m trying to use the value provided via the “output” parameter (-o) to replace a path that’s referenced in one of my source files. That file happens to be a Jenkinsfile that needs to know where a sln file is located in order to build it. The jenkinsfile will live in the same directory as my sln.

When I don’t specify a value, I’m fine with falling back to my default (since the developer knows to scaffold the solution in a directory which matches the default).

My ultimate goal is to enable a developer to run the template engine, check in the source, and build the application through jenkins via a jenkinsfile.

Using the code below, you’ll notice that you ALWAYS get the default value when you invoke the template to generated the transformed Jenkinsfile. I’m unaware of another way to do this (tried using an evaluate and also tried using “target” instead of “output”).

Am I missing something or is this just a gap?

Here’s the sample template.json

{  
  "$schema": "http://json.schemastore.org/template",
  "author": "Test",
  "classifications": [
    "Web",
    "WebAPI"
  ],
  "name": "TEST ASP.NET Core Web API Solution",
  "identity": "TEST.WebApi.SolutionTemplate",
  "shortName": "test-webapi-sln",
  "sourceName": "WebApiTemplate",
  "preferNameDirectory": true,
  "tags": {  
    "language": "C#"
  },
  "primaryOutputs": [
    {  
      "path": "WebApiTemplate.sln"
    }
  ],
  "symbols": {
    "DefaultSourceDirectory": {
      "type": "generated",
      "generator": "constant",
      "parameters": {
        "value": "src"
      }
    },
    "SourceDirectoryReplacer": {
      "type": "generated",
      "generator": "coalesce",
      "parameters": {
        "sourceVariableName": "output",
        "fallbackVariableName": "DefaultSourceDirectory"
      },
      "replaces": "templates/test-webapi-sln"
    }
  }
}

And the Jenkinsfile which it works on

pipeline {
  agent none
  options {
    skipDefaultCheckout true
  }
  stages {
    stage('Compile and test') {
      agent {
        label 'dotnet-2.2'
      }
      steps { 
        checkout scm
        stash 'source'
        dir("$WORKSPACE/templates/test-webapi-sln"){
          echo 'Building WebApiTemplate.sln...'
          sh 'dotnet build WebApiTemplate.sln'
          echo 'Running unit tests...'
          sh 'dotnet test UnitTests/test.WebApiTemplate.UnitTests.csproj --no-build'
        }        
      }
    }
    stage('Build docker image') {
      agent {        
          label 'docker'
      }
      steps {
        unstash 'source'
        dir("$WORKSPACE/templates/test-webapi-sln"){
          sh 'DOCKER_REGISTRY=072100331905.dkr.ecr.us-west-2.amazonaws.com/ IMAGE_VERSION=0.0.0.$BUILD_NUMBER docker-compose build'
        }
      }
    }
    stage('Login to ECR') {
      agent {
        label 'aws-cli'
      }
      steps {
        echo 'Generate script to enable docker-compose push to ECR'
        sh 'aws ecr get-login --no-include-email --region us-west-2 > docker-login.sh'
        sh 'chmod +x docker-login.sh'
        stash includes: 'docker-login.sh', name: 'docker-login'
      }
    }
    stage('Publish docker image') {
      agent {        
          label 'docker'
      }
      steps {
        unstash 'docker-login'
        sh './docker-login.sh'
        unstash 'source'
        dir("$WORKSPACE/templates/test-webapi-sln"){
          sh 'DOCKER_REGISTRY=072100331905.dkr.ecr.us-west-2.amazonaws.com/ IMAGE_VERSION=0.0.0.$BUILD_NUMBER docker-compose push'
        }        
      }
    }
  }
}

When I run dotnet new -o output, I’d expect that templates/test-webapi-sln would be replaced with output, but instead, it is replaced with src (the default value in the coalesce macro).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bekir-ozturkcommented, May 19, 2021

We have discussed this internally and found the concerns valid. We want to extend this to other useful built-in parameters such as fullOutputPath, hostIdentifier, hostVersion etc. For the time being, we are aiming for .NET 7.

0reactions
vlada-shubinacommented, May 16, 2023

We have discussed this internally and found the concerns valid. We want to extend this to other useful built-in parameters such as fullOutputPath, hostIdentifier, hostVersion etc. For the time being, we are aiming for .NET 7.

Update on this: it is already possible to use HostIdentifier and HostVersion (for dotnet new) via bind symbols, however output path is still not accessible. Adding outputPath as implicit variable (as name for --name) has to be considered. At the moment there is no timeline for that unfortunately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fallback variables in `dotnet new` templates - PatridgeDev
In this post, we'll create a template symbol that will function like a coalesce operator, taking a preferred input but falling back to...
Read more >
NET default templates for dotnet new - .NET CLI
To list the built-in templates, run the dotnet new list command: ... The following table lists the default values according to the SDK ......
Read more >
Migration to PHP 8.1 - how to fix Deprecated Passing null ...
My main problem with this approach is, that the function rename_function(PECL apd) no longer works, last update on this is from 2004 1....
Read more >
COALESCE | InterSystems IRIS Data Platform 2023.2
The COALESCE function evaluates a list of expressions in left-to-right order and returns the value of the first non-NULL expression.
Read more >
COALESCE(exp1, exp2, ...)
Returns the first non-null expression from the list of supplied expressions. If all expressions are NULL, returns NULL. The data types of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found