12. Create Salesforce DX Plugins | Shane McLaughlin


dx plugin

In this talk, Shane shares what Salesforce DX plugins can do and how to create it to facilitate developers’ work.


Main points

  1. Why did Shane create the shane-sfdx-plugins open source project?
  2. Two groups of plugin commands in the plugin
  3. Why not incorporate the dx plugin into the core DX
  4. Oclif framework that shane-sfdx-plugins is built upon
  5. Discuss how the two groups of plugin commands work under the hood
  6. How to use this plugin to install both 1st and 2nd generation packages
  7. Using 2nd generation package in customer projects
  8. What Shane has learned from creating this plugin
  9. Should Salesforce developers create DX plugins too?
  10. Discuss current Salesforce developer experience v.s. Before

Links


Contact Me

If you enjoy this podcast, please reach me out to let me know your thoughts or simply say Hello. My (Xi’s) contact info:

Take care, until next time :)!

Transcript

AI-generated, unedited.

[00:00] Salesforce Way podcast, episode twelve, the first episode in 2019. Hey,

[00:19] thanks for listening to the show. In this episode, I'm talking with Shane McLaughlin about his open source DX plugin. Why does he create it? What problems does it solve? And what has he learned from building this project? Hopefully, you find our chat interesting and useful. By the way, during this session, I had one misconception. I thought the unlocked packages was still not in GA yet. But that was wrong. In reality, it's already in GA. It's the other type of second generation packaging called managed packages. That is for the ISV, and that is still in beta as of today, in February of 2019. So don't get confused when I ask this wrong question during our session. I do apologize for this. If you have anything to say to the show, you can reach me out by email at SalesforceWay twenty nineteen at gmail dot com. Also, my social media accounts can be found at the contact page of the podcast website, SalesforceWay dot com. Without further ado, let's start the show today.

[01:48] Hello, everybody. This is Xi Xiao. This is yet another episode of Salesforce Way podcast. Today, I'm sitting with Shane McLaughlin together, and we are talking about DX plugins. Hello, Shane. Hi, Xi. Would you like to introduce yourself before we dive into our topic? Yeah, sure. So my name is Shane McLaughlin. I live in Houston and I work for Salesforce and have for about four and a half years. My current job, I am a developer for Platform Product Marketing, and so that means I get to be pretty much where customers are. I get to build with the platform quite a bit, and then also get to publish a lot of open source tools. And that's one of the things that that she reached out about and wanted to talk about. Yeah, so you know, I had this requirement that I need to create a lot of custom objects, custom fields. Then I googled around to see if there there were any tools to help me. Then I I found out your tool. It's the Shane SFDX plugins. It's open source repo in GitHub. And then I started to you know play around with it, and then it really helped me a lot on that requirement. So I don't have to manually go to the UI to create the custom object or custom fields. So everything was like automated with the command line. So that was a big help. And this is also the the motivation that I want you to be in the show, be the guest, and share your development

[03:19] experience with everybody. Great. So if we start our topic today, so it's about the DX plugging. My first question is: Why do you create these sharing SFDX plugins? Was it because of your work needed, or were you just want have a hobby? You know, to play around with the plugging features. It was definitely part of work projects. If you if you look through this bunch of plugins, there's kind of two flavors of them. Some are meant for what I call dev time, like the one you described, where you're creating objects and fields. It was a way to make that work a little bit more efficient and to be able to have all of that source without having to go do it in the UI manually and pull it down. And then there's another group of plugins that that kind of came out of another project I was working on. And so one of the things I did with SFDX is I created this thing called a deployer, which you can point it to an open source SFDX project, and it will spin up a scratch org and run scripts and build all that stuff in it. And so you know, for example, one of the things that SFDX lets you do is to generate a random user password. But it doesn't have a plug-in to specify a user password, so you can get a really ugly password, but you can't get a nice password. And so you can write a plug-in like that, and that's really for that deployer to be able to give someone a scratch org and a password that they can

[04:49] successfully type in their phone on the first try. Ah, and also I guess that also a help on the automation part, right? The user could define in advance what's the username and password, and already part of the script. Yes. Yes. Exactly. So, you know, for example, SFDCX is pretty good at opening something in your default browser. But if you said, "Hey, I want to run this and test it in three different browsers," you would have to reauthenticate to that org from each browser. You know, kind of using the either the web process or JWT or something. So, if you are trying to open things in a browser, that makes it a little bit easier. Okay, I understand the Salesforce, the company and the the entire Salesforce platform is more towards the open source. We embrace. More publicly, the open source world, but in this case, you said it's somehow related to your work. Why not then DX just embrace and you know integrate all these functionalities into its own DX zone? Why has it to be like open source and other plugins? That's a that's a really good question. So part of it is I don't work on the team that specifically builds SF DX. And so for them, I'm more of a customer. I'm one of their users. I just happen to work inside the company, and so I I know where their code is. But they have a roadmap and they have plans. And so I'm I'm sure if I went

[06:19] in and and made them a pull request, they they might accept it. They may have something that's already in progress for it, and then they may have like some of the things I've built. They've said, hey, we will never ever use that. And so, yeah, I expect that some of this may get incorporated into the core DX plugins. Like we've had some of those conversations, but they're also in the process right now of doing a pretty big, pretty big rewrite of SF DX to use all the. I don't know if you've seen the Oakleaf framework, just to make sure that everything there aligns with Oakleaf. And so I'm trying not to disrupt their stuff right now. Okay, I've never heard about this news. They're rewriting the whole thing. It sounds yeah exciting. I don't know if it's like internal news here, but it sounds really cool. Yeah, Oakleaf is. If you haven't played with it, that's what the plugins that I've built and the plugins that everybody else has built are running on. But because the core SFDX, you know, the official SFDX commands have been out for a little while, a couple years, they weren't. Oakleaf wasn't available at the time, and so there's some there's some things that Oakleaf has that are really nice that that we want to incorporate into it. Like for example, autocomplete.

[07:36] Oakleaf gives you that. So if you go create your own command that's not related to SFDX, you'll have autocomplete on it. And so we want to have autocomplete. And when this thing moves to Oakleaf, not only do you get autocomplete on the SFDX commands, but all of the SFDX plugins that you would install, like mine or like something you would build, they'll also have autocomplete on them as well. Okay, yeah, I remember I I watched some like a DX promotion videos before, and the Oak Cliff was definitely already you know mentioned there. But because for me or for most of the Salesforce developers, it's still not like the mainstream. It's not our daily work. So I tend to you know okay, this is a cool feature. I had look at the page, but I didn't really dive into that. So maybe that's more of like a advanced the platform related developers who are closely working to to those features like you are doing now. Yeah, when I originally built this, Oakleaf wasn't out yet, and so I remember taking all of my stuff and moving it to Oakleaf. So if you were to go through that GitHub project and go way back in time, you would find that. And so a lot of it was just getting rid of code. There were things that I was having to validate flags and be like, oh, if you use this flag, you can't use this flag. So I had like logic to test that and then show the error, you know, tell the user. What they did wrong, and with Oakleaf, you can say, "Hey, if you use this flag,

[09:07] it depends on this other flag, or it's exclusive of this other flag." So even just flag parsing was a was a huge improvement, and so getting all of the getting all of the core code onto that is going to make things more consistent and maintainable and everything. Okay. If we go back to your plugin, I'm opening in my browser. You mentioned the commands in the plugin basically split into two groups. One is like we can create a custom object or fields. Another is like install or deploy from GitHub or Heroku features like that, right? Yeah. So. Let's start with the creating custom object or fields. Those features. How does it really work under the hood? I understand we created some metadata files in your local environment, and then you use the DX plugin to push into the scratch org. Does it work like that? It does. Yeah, and those are two separate steps. So you might want to create an object and a whole bunch of fields, and not push it for a while. And because there's already a push or deploy command, so the idea is to separate those two things out. So you're creating schema, and then eventually you decide, yes, I'd like to push this to an org now. Ah, okay. And they have a there's kind of two options with those commands. One is you can pass in a whole bunch of flags to pretty much set the whole field or object up. And there's also a a dash i,

[10:37] which is the kind of the interactive flavor where it's going to ask you and provide you defaults and kind of like when you run like npm in it and it asks you some questions. I wanted to do that with custom objects and fields because then if you're like, oh, is this is this a custom object or is it a platform event? You know, or what? What type of field is it? Oh, and if you say text, well, I need to know how many characters you want. But if you just hit enter, you're going to get two fifty-five, for example. Okay, so the interactive way is more. Let's say the user go there to run the command on the fly. But the the other way you you set all the parameters in advance. That's more for automations, I would assume, right? You have everything in place already. I think so. Or for someone that really knows it, but for me, is sometimes it's easier just to take those defaults. I think it would be more work to use all the flags. But it's they're there. Okay. Okay. Good. Then, how about the the the other parts? The install and the deploy via Heroku or GitHub. How does it really work? So, if you've used SFDX packaging, what we're calling Generation Two packaging, there's an SFDX project JSON file that gets created, and as you create packages and package versions, your project JSON file starts to have those in it. And so then, if you have that package, if you have that repository up on GitHub, a lot of what I've done

[12:07] is to break apart packages that I reuse. So I may have several workshops or demos that may use some common functionality. So what I'll do is I'll put that in a package. And so then, if you want to install that in another project as part of its setup script, you would normally need those package IDs, and you would say. You know, hey, install package, and it starts with O four T. That's a package version. Okay. And if you ever create a new version of your package, so let's say it's the Spring nineteen version, then you have the you know the work of going and finding all the code, all the scripts that were using the old version of the package. And so, what I did with GitHub package install is you just pointed at a repo. You know, you give it a user GitHub username and a GitHub repo name. And what it does is it goes and finds that project JSON file, and then finds the most recent package version and installs that. And so it's kind of like saying, you know, on npm world, give me the latest. Hmm. And so, because yeah, yeah, go ahead, please. I was going to say because the that project version or the package version is always in the project JSON file. You can just go read that file as long as it's public. Okay, but this only works with the second generation package, right? Um, I think you could make

[13:37] it work with first generation packages. You would just have to get them into that package JSON file, which SFDX doesn't do by default. You'd have to go add the ID manually. Okay, so it means in this case, not all the let's say open source Salesforce repos in GitHub is installable. It only supports the ones with certain formats. We mentioned the JSON format. The JSON file should be in place in a certain a path in a repository. Yeah, so that's the the one that says GitHub package install. That one pretty much pretty much uses packaging too. The sec the the one after that that's the GitHub source install. That one is meant to deploy something that's not packaged. So, for example, it's just metadata API format code, and and what all that command is doing is it's going out and basically cloning that repo into your local, and then doing a metadata API. Deploy to whatever your scratch org is, and that lets you get around things that are maybe metadata API deployable but not packageable. Or, for example, it's someone who wrote some code that you want to use, and it's it is on GitHub. And it's in the old format, and it doesn't use packaging, so that's what I use that command for. All right. So in this case, I would assume the package. The XML file is used for, you know, yes, uploading on the on the metadata. Okay. Yeah, you just

[15:07] tell it which GitHub repo, and then you tell it what the folder is where the source lives. And so, like for example, if you were using Maven's Mate previously, you know, you'll have a slash src folder. With the package XML in it. So, but if your if your folder is named something else, you can just say, "Oh, my folder is called something else," and it'll get that and deploy it for you. Okay, this sounds quite convenient. At least we can play around and install with the two generation package from from the DX command line. Yeah, and the idea is, if you make a change to your package, you just update your package repo, and you don't have to update all the downstream projects. And then you just run the DX install again. Then you get the latest version to your environment. This sounds quite handy, and it looked quite like, as you mentioned, the npm package for JavaScript, right? Everything is a module, and then you define the package versions, and then install specifically that version to your your environment. Yeah, the version support is something that I I don't really have. Okay. So like by yeah by default these things are going to pull the most recent package. So if you if you mean to point to a specific package version, you probably just want to go get that package ID and use the standard SFDX command that's package install, and you pass in that ID, and that way it's always always the same version. So what you get with what

[16:37] you get with package.json is you'll have a package ID. So if you if you publish stuff on npm before. Never. Okay, so basically, what you have is, you know, your code is in some sort of state, and you've made your commits, and then you run npm package, and then maybe it's major, minor, or version, and then you get, uh, it creates a package, and it creates a version of that, and so at that point. It's sort of a snapshot of the commits at that time, and then if you do some more changes to it, you can always make another version of your package. And so the the idea with packages is that they're more stable than

[17:21] if more commits come in, you don't necessarily get those unless you want them, I guess. And so then what I do with my plugin is is is something in between where it's like I want a package, but just give me the latest package. Even if there's been more, like for example, there may be some more commits. There may even be commits to master that haven't been like packaged yet. In which case, you wouldn't get those. You're just getting whatever the last package was. Ah, okay, okay. Um, so we talked about the second generation package, and I've heard a lot of like companies and developers they start using that. But it it didn't go GA yet, right? As I remember, so we're still waiting for like the general support, the public support. So, do you recommend we start to play around with that and get familiar with it, or do we just wait? What's what's your opinion on this topic? It depends what your what your use cases are. So, for example, if you're a customer, first generation packaging is not very useful for you. Because you have either this unmanaged package that you're sort of deploying over itself, and they don't upgrade very well, or you have to go to the fully managed package where it's hard to deprecate things. And like that was really meant for ISVs on the App Exchange. And then you know once you declare something global, it's sort of you're stuck with it forever to avoid breaking

[18:52] APIs and stuff. And so for most of the most of the folks I talk to that are not you know. Doing ISV type work, second generation packaging is the way to go, and it gives you it gives you the unlocked packages option. So then you can deploy stuff over existing source, but now it's it's coming from the package instead. If you're doing any sort of CI stuff and you want to have stable. Dependencies and stuff like that. The packages are super good for that. And so, in in Salesforce world, beta is usually meant to be, hey, this is production quality. We're just not done with it yet. We're we're not intending to make breaking changes, but there's some like serious missing features to it. And and so, beta is something that's like still still in progress. But we don't call it beta if it's not usable. Are meant to be used by customers. Okay, yeah, I mean just to learn and play around with it. Definitely, I'll I will go with the the this second generation package. But for like for the customer project, it's really hard to convince them to start using this unlocked package. You know, for the Salesforce org development. Because you need specifically flag that the that enable the second generation package, and there's a warning over there. So, customers won't really allow us usually to to

[20:22] enable that because of this warning message. Yeah, because it's beta, or because they just don't like the warning message itself. I think both, but more is like it's a beta. Yeah, it's more like the beta thing. Like back in the days when DX, I think was the scratch org was not, you know, was still in testing beta, and we also need to enable that, like click the flag. I don't remember now. Do we still in in Salesforce work? Do we need to enable the scratch org, the Dev Hub, let's say? But that's that was always the thing, you know, for customers. Should do we do that or not? Usually, they just go with the normal, routine ways. They don't really go with the cutting edge features. Yeah, yeah. I think this is one of those scenarios where you know there are some some really good third party tools for dealing with org diffs and deployments and stuff like that. But if if you were you know if I were a customer today, and I used to be a customer for four years before I worked for Salesforce. Okay. And you know we had we had built some some pretty, I guess, for the time. So we were running a Jenkins server. We had. Repositories and GitHub that were private, and when you check something into QA, it would deploy everything to our full sandbox. So we were using you know version control and GitHub and the the Ant. Metadata deployments and stuff, and so if I were given the choice of of doing you know all of that setup work or kind of going

[21:52] and and using second generation beta packages, I think as a customer I probably would have gone down. What what DX currently has for for as much as I could because what we ended up doing was starting to look a lot like packages where we had package XML files for segments of our code. And so, for example, everything that had to do with this was a energy company, but you know, for everything that had to do with our electricity portal, we just had a package XML that listed out all those files. And so, if you ever added a new Apex class, we would just add it to that one package XML. And so that in Jenkins, I could say deploy all the electric portal stuff, and it knew what all that metadata was. And so that was almost functioning as like a mini package. Yeah. Okay. All right. If we now go back to the DX plugging topic, and so you have. Played a lot with the package features, and you created a lot of the functionality as you as we just discussed. So, what do you have learned from you know all these practices? Do you recommend Salesforce developers to spend time on learning this stuff, or we just do the things that we are supposed to do? You know, create the the code that are deployed to Salesforce work. Yeah, I think it all depends on where you find yourself doing repetitive work, and maybe where you find yourself.

[23:23] Finding things that you can automate, and so, you know, just to kind of go through some of the other things on the plugin. One of the one of the things that's really common is I get an org that wasn't built with SFDC, and I feel like kind of getting that up to. Updated, and so one of the things that you get there is these really large profiles that you know, like for example, you pull down the admin profile and it's got access to like everything in the whole org, and so, and then I would what I was doing is I was taking that and creating a permission set. And then pulling that down, and then I would start cutting and pasting stuff out of the XML profile into the permission sets. It's like, okay, these fields are going to go in this permission set. These fields are going to go in this permission set. And then this stuff we're not moving at all. And so after doing that, I don't know three times. That's the point at which I'm like, no, this should be a plugin. I should be able to say, hey, take this profile and turn it into a permission set. Right. Same thing with you know, for example, even some of the stuff is really simple. Where if you if you've done a lot of Lightning components, you've seen we have this Lightning component library, and there's a public one, but then there's one that lives in your org that knows about all your custom components that you've built. Yeah, and for whatever I you know, if you've seen this, one of the challenges is there's not a way to get to it in setup unless you actually know the URL for it, and I can never remember what the URL is, and so I just made a

[24:53] I just made a plugin that just opens that. Ah, okay, right. So it's simple. It's probably like it's probably one of the simplest plugins here, but. I don't have to remember that URL anymore. It just sort of saved me a little bit of work. I got it. I got it. Yeah, but to go to your question of, you know, what did I what did I learn out of this? One of the things I learned out of this that was really useful was this was my first serious TypeScript project. So I've been I've been doing JavaScript for you know years and years and years, but TypeScript was always something that was like, oh, that's kind of cool. I should probably check that out at some time. And I'm like, oh, I read the docs and kind of looked at it. But until you kind of live in one for a little while, sometimes you don't appreciate something as much. And then you know, once once you get that, you know, writing. Writing TypeScript code is is pretty fantastic. I'm sort of in love with it, and so there's there's been some other. Anytime the JavaScript projects get kind of big, a lot of times I'll go through and switch them over to TypeScript just because I had such a good experience on this part. Uh huh. Okay. So the DX plugin, we need to use TypeScript instead of the regular JavaScript way. It's optional because the TypeScript does compile to JavaScript, and so I think in the there's a whole plugin guide. I don't know if you've seen that. It came out I think within the last release. But there's an SFDX plugin development guide, and I think it's going to give you both options.

[26:24] You know, for me, the TypeScript was was kind of was nice, and I think a lot of the examples are written that way, and a lot of the a lot of the actual plugins are written that way. And so, when I go like look at other people's code to find things, they tend to be in the TypeScript format. So, if you're if you like TypeScript, then you should probably use it. If you don't know TypeScript, it's probably a decent place to take a look because a lot of plugin projects start off small enough that it won't you know it won't be too hard to pick up. Hmm. Good. In this podcast, actually, we always recommend and encourage the developers to really look out of the box from the Salesforce development. There's a lot of things we can learn, like create the package and learn from the other industries as well. So, yeah, as you said, it brings values and it kind of we round your software engineering skills from the other angles. You know, I joined Salesforce the the world like about a year ago, and I just made the decision. Okay, I want to learn a bit more about Salesforce. So I jump in from like Microsoft stack to Salesforce, and I do see a lot of things happened even the past one year. So it's like amazing. I don't know. Was like the company shifting the direction, or I mean, I I used to be like a founder and a developer, and then okay, we have the Lightning component coming up, and then we have the

[27:54] Lightning Web component, we have the DX, all these things, and including, for example, the version controlling the Git. So all these things are are. From the other industry, they are standard. They are part of the automation, continuous integrations, and now Salesforce is really following the trend, and then you know just catch up and do all this cool stuff. So that makes me as a Salesforce developer more happy and more proud of these these things I'm working on. Yeah, that's kind of a two-part problem, and so one of them was people like you who are coming to Salesforce. Sometimes they look around and they expect to see certain things, and those certain things never existed on Salesforce, and so they either get frustrated or you know they have like a big learning curve before they can really be productive, and you know. There's there's some special things on Salesforce that really do take some time to learn. So for example, if you come over and decide to code everything, you're you're probably doing it wrong if you don't really know what all you you don't have to code. Yeah, but in terms of you know you come over and you say oh I should be able to use pretty much any IDE I like, you know, and I should be able to do version control and some other things that were really not part of Salesforce. Or if they were, you sort of had to do a lot of A lot of setup and a lot of architectural decisions to decide how to do such things. So we really wanted to make that stuff normal, and you'll see that theme a lot with

[29:24] Lightning Web Components. You know, it's it's really pretty much the standard web components from the HTML. We're trying to use standard events and a whole bunch of other standard stuff, so that if you were a JavaScript developer and building web components, you would come over and you would recognize eighty percent of it. And the parts that you wouldn't recognize would be, you know, how do I interact with Salesforce servers? You know, get records, get metadata, save data, do internationalization. T here is some things that that really have to be tied to the platform, but those should at least look normal. You know, I am making a. I'm making a call. I'm getting back something, and then everything else it should just be like regular JavaScript. and And Aura was definitely not like that. It sort of had a big learning curve with the the markups. The markups having, you know, you're almost declaring variables in markup. You know, so I think the the two things that gives us is is one, it's it's easier for developers to learn, and then it's also easier for developers. It makes the pool of talent bigger, which sometimes is a challenge for our customers. They're like, I like Salesforce, but it's hard to find someone who knows all this special Salesforce stuff. And so our idea is, hey, I could teach you how to use the services from JavaScript in your components, and then you can just go be a productive JavaScript developer. The other thing that we have that we're really trying to embrace is the tooling ecosystem. Okay. So, for example, on Lightning Web Components,

[30:55] that's really about oh, browsers are optimized to run web components. It's part of the standard. So there's sort of an arms race to do those as quick as possible. You shouldn't have. Tons of framework code running on it, but if you go look at what we're doing with some of the SFDX tools, like even Apex is now described as a as a language server. And so, if you have to write Apex and you're doing it in Visual Studio Code, you know you get linting and and like. Autocomplete and you know you can peek at the definition of something, and and that's all done with this language server, which is also a like a standard thing, and so. We built one for VS Code, but if you went to some other IDE that didn't have it, you could just say, "I just need the Apex language server," and then you wouldn't have much work to do to put that in whatever IDE people will be using three years from now. Yeah, yeah. So, yeah. Just to recap, um, I understand the language server is like the middle well standing between the Salesforce platform and our our toolings, and it's kind of abstracted layer that it can talk to all different ah tools that you are using as long as it's aligned with the. The API of the language server, then the the tool should be able to support yeah development. Because what what used to happen is people would write IDE plugins and they would have to get all of the Apex commands and the Apex grammar and kind of describe that. And so now it's easier. Most of the tools

[32:25] now support language server. So it's yeah, it's this nice little in between thing that all the tools can speak. And then we can show them our language. And then you know when you add something like the Apex case state or switch statement, I can't say case the switch statement. We just add that to the language server, and then all the IDEs can have it that are using that protocol. So it does make things a lot easier to maintain. Yeah, got it. All right, I think that's all for our session today. Before let you go, do you still have more other stuff to to share with us? Um. Yeah, I will. I'll just say the. I'll give you my GitHub username. That is M as in Michael, and then Shane, and then MC as in McLaughlin. So you'll find a bunch of stuff there. But I kind of pinned the most important ones. So the SFDX plugins. There's also the the thing that I'm using for scratch org deployers. And yeah. So you'll so you'll find important useful things there. Yeah, and it's got. All the documentation, which is another thing that we didn't talk about with the plugin, but like there's this README page that has like a list of all the commands. And all those are generated for me from the plugin generator. So it's it's a it's a neat little thing. Ah, you mean the the plugin generated this readme document for you? Ah, yeah. There's so you know when you type a command, you can put dash h. And then, yeah, like, yeah. When I write a command, I can, yeah. So what it's doing is it's grabbing all the commands

[33:55] and pulling out all the help and example text from those commands, and then it builds this whole section of the README for you. Ah, okay, then I got it. Yeah, definitely. So Shane, I will put your GitHub profile link in our show notes, and also definitely our topic, the DX plugin repo as well. So, anybody who is interested in this topic, definitely go there to the episode page and check it out. All right, and thank you for thank you for having me. Really appreciate the perspective for someone new to the platform, and please give us lots of feedback. Yeah, you're welcome, Shane. So talk to you later then. Bye bye. All right. Bye.

Leave a Reply