-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new library manual draft and samples #110
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
341cd00
Add new library manual draft and samples
cartermp 3bf3682
Responding to PR feedback
cartermp da442ac
Update new-library project.json to work for rc1-final
cartermp a9b1d8d
Add testing instructions and update new-library sample
cartermp 3df4359
Add multiple project text and samples
cartermp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
484 changes: 484 additions & 0 deletions
484
docs/tutorials-samples/targeting-core-for-new-libraries.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# .NET Core Library Samples | ||
|
||
These samples can be built and run using the .NET Core rc1 toolchain. | ||
|
||
They are small and intended to demonstrate how to target and build NuGet packages for different targets. | ||
|
||
To build/use any of these (using `new-library` as an example): | ||
|
||
1. Open your favorite Command Line Interface. | ||
|
||
2. Navigate to the top-level directory: | ||
|
||
`$ cd new-library` | ||
|
||
3. Restore packages by typing the following: | ||
|
||
`$ dnu restore -s "https://myget.org/F/xunit"` | ||
|
||
`$ dnu restore` | ||
|
||
Note: The first `dnu restore` is needed right not to allow for unit tests to run. This will not be required in the future. | ||
|
||
4. To build and package the library as a NuGet package, type the following: | ||
|
||
`$ cd src/Library` | ||
|
||
`$ dnu build` | ||
|
||
`$ dnu pack` | ||
|
||
Check out the `/bin/Debug` directory to see the generated artifacts and `.nupkg`. | ||
|
||
5. To run unit tests (only applicable to `new-library`): | ||
|
||
`$ cd ../../test/LibraryTests` | ||
|
||
`$ dnx test` | ||
|
||
And that's it! | ||
|
||
## new-library | ||
|
||
The project under `/new-library` is a project targeting **only** .NET Core. Backwards compatibility is not a goal. | ||
|
||
It also showcases two other things: how to use multiple projects and how to test. | ||
|
||
There are two libraries showcased. The first, `DependencyLibrary`, contains functionality that the second, `Library` uses. The `LibraryTests` test project takes a dependency on `Library` to test that project. | ||
|
||
## net45-library | ||
|
||
**IMPORTANT:** This project requires Windows and .NET Framework installed on your machine. | ||
|
||
The project under `/net45-library` is for targeting any of the following: | ||
|
||
* .NET Framework 4.5 and above | ||
* Windows Phone 8.1 | ||
* Whindows Phone Silverlight 8 | ||
* Universal Windows Platform | ||
* DNX Core | ||
* Xamarin | ||
* Mono | ||
|
||
It uses the new `dotnetXX` Target Framework Moniker introduced with the .NET Platform Standard. | ||
|
||
## net40-library | ||
|
||
**IMPORTANT:** This project requires Windows and .NET Framework installed on your machine. | ||
|
||
The project under `/net40-library` is for compatibility with .NET Framework 4.0 and above. It also demonstrates how to use `#if` compile guards to multi-target for a .NET 4.0 target. | ||
|
||
## pcl-library | ||
|
||
**IMPORTANT:** This project requires Windows and .NET Framework installed on your machine. | ||
|
||
The project under `/pcl-library` is for compatibility with a supported PCL Profile (e.g. 3344). It shows how to structure the `project.json` file to allow for targeting a PCL. It also demonstrates how to use `#if` compile guards and how to set those up in conjunction with the `project.json` file. |
53 changes: 53 additions & 0 deletions
53
samples/core-projects/libraries/net40-library/src/Library/Library.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
#if NET40 | ||
using System.Net; | ||
#else | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
#endif | ||
|
||
namespace Library | ||
{ | ||
public class Net40CompatLibrary | ||
{ | ||
#if NET40 | ||
private readonly WebClient _client = new WebClient(); | ||
private readonly object _locker = new object(); | ||
#else | ||
private readonly HttpClient _client = new HttpClient(); | ||
#endif | ||
|
||
#if NET40 | ||
public string GetDotNetCount() | ||
{ | ||
string url = "http://www.dotnetfoundation.org/"; | ||
|
||
var uri = new Uri(url); | ||
|
||
string result = ""; | ||
|
||
lock(_locker) | ||
{ | ||
result = _client.DownloadString(uri); | ||
} | ||
|
||
int dotNetCount = Regex.Matches(result, ".NET").Count; | ||
|
||
return $"Dotnet Foundation mentions .NET {dotNetCount} times!"; | ||
} | ||
#else | ||
public async Task<string> GetDotNetCountAsync() | ||
{ | ||
string url = "http://www.dotnetfoundation.org/"; | ||
|
||
var result = await _client.GetStringAsync(url); | ||
|
||
int dotNetCount = Regex.Matches(result, ".NET").Count; | ||
|
||
return $"dotnetfoundation.org mentions .NET {dotNetCount} times in its HTML!"; | ||
} | ||
#endif | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
samples/core-projects/libraries/net40-library/src/Library/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"frameworks": { | ||
"dotnet55":{ | ||
"dependencies": { | ||
"System.Runtime":"4.0.0-rc1-*", | ||
"System.Net.Http":"4.0.0-rc1-*", | ||
"System.Threading.Tasks":"4.0.0-rc1-*", | ||
"System.Text.RegularExpressions": "4.0.0-rc1-*" | ||
} | ||
}, | ||
"net40":{ | ||
"frameworkAssemblies": { | ||
"System.Net":"", | ||
"System.Text.RegularExpressions":"" | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/core-projects/libraries/net45-library/src/Library/Library.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace Library | ||
{ | ||
public static class Net45CompatLibrary | ||
{ | ||
public static string Hello => "Hello .NET Core and .NET Framework 4.5!"; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
samples/core-projects/libraries/net45-library/src/Library/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"frameworks": { | ||
"dotnet51":{ | ||
"dependencies": { | ||
"System.Runtime":"4.0.0-rc1-*" | ||
} | ||
}, | ||
"net45":{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"projects": [ | ||
"src", "test" | ||
] | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/core-projects/libraries/new-library/src/DependencyLibrary/DependencyLibrary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace DependencyLibrary | ||
{ | ||
public static class NewDependencyLibrary | ||
{ | ||
public static string ImportantMessage => "Live long and prosper"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/core-projects/libraries/new-library/src/DependencyLibrary/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"frameworks": { | ||
"dotnet55":{ | ||
"dependencies": { | ||
"System.Runtime":"4.0.0-rc1-*" | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
samples/core-projects/libraries/new-library/src/Library/Library.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using DependencyLibrary; | ||
|
||
namespace Library | ||
{ | ||
public static class NewLibrary | ||
{ | ||
public static string Hello => "Hello, .NET Core!"; | ||
|
||
public static string GenerateImportantMessage(string name) => | ||
$"{NewDependencyLibrary.ImportantMessage}, {name}!"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
samples/core-projects/libraries/new-library/src/Library/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"dependencies": { | ||
"DependencyLibrary":"" | ||
}, | ||
"frameworks": { | ||
"dotnet55":{ | ||
"dependencies": { | ||
"System.Runtime":"4.0.0-rc1-*" | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
samples/core-projects/libraries/new-library/test/LibraryTests/LibraryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Xunit; | ||
using Library; | ||
using System; | ||
|
||
public class LibraryTest | ||
{ | ||
[Fact] | ||
public void HelloTest() | ||
{ | ||
string expected = "Hello, .NET Core!"; | ||
|
||
string actual = NewLibrary.Hello; | ||
|
||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Fact] | ||
public void ImportantMessageTest() | ||
{ | ||
string expected = "Live long and prosper, Captain Kirk!"; | ||
|
||
string actual = NewLibrary.GenerateImportantMessage("Captain Kirk"); | ||
|
||
Assert.Equal(expected, actual); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
samples/core-projects/libraries/new-library/test/LibraryTests/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"commands": { | ||
"test":"xunit.runner.dnx" | ||
}, | ||
"frameworks": { | ||
"dnxcore50":{ | ||
"dependencies": { | ||
"Library":"", | ||
"System.Runtime":"4.0.0-rc1-*", | ||
"xunit":"2.1.0", | ||
"xunit.runner.dnx": "2.1.0-rc1-*" | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
samples/core-projects/libraries/pcl-library/src/Library/Library.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
|
||
#if !PORTABLE259 | ||
// .NET Core libraries can go here. | ||
#else | ||
// Use a library here that is compatible with PCL Profile 329. | ||
#endif | ||
|
||
namespace Library | ||
{ | ||
public static class PCL329CompatLibrary | ||
{ | ||
public static void Foo() | ||
{ | ||
#if !PORTABLE259 | ||
// Using a .NET Core library unavailable to PCL Profile 329 | ||
#else | ||
// Need to use a a PCL Profile 329-compatible library here. | ||
#endif | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
samples/core-projects/libraries/pcl-library/src/Library/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"frameworks": { | ||
"dotnet55":{ | ||
"dependencies":{ | ||
"System.Runtime":"4.0.0-rc1-*" | ||
} | ||
}, | ||
".NETPortable,Version=v4.0,Profile=Profile344":{ | ||
"compilationOptions": { | ||
"define": [ | ||
"PORTABLE259" | ||
] | ||
}, | ||
"frameworkAssemblies":{ | ||
"mscorlib":"", | ||
"System":"", | ||
"System.Core":"" | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a note on how to build release?